JavaFX默认使用jdk的程序图片显示,可以通过以下代码进行修改设置
stage.getIcons().add(new Image("static/icon.png"));
static/icon.png改为自己图片路径
这里可以使用相对路径和绝对路径,看自己需求设置
例:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
//设置软件图标
stage.getIcons().add(new Image("static/icon.png"));
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}