对于 linux 如何在我的应用程序的 java fx 1.8.45 中的舞台标题栏上设置图标

for linux How set icon on the title bar of a stage in java fx 1.8.45 of my application

我试过了

new Image(XYZ.class.getResourceAsStream("my.png"))

stage.getIcons().add(new Image("my.jpg")); 

stage.getIcons().add(new Image("/path/to/my.jpg"));

我可以在任务栏看到图标,但在应用程序标题栏看不到 我的 javafx jar 版本是 1.8.45 谢谢

尝试以下方法,这对我有用:

Image image = new Image(XYZ.class.getClassLoader().getResource("my.png").toExternalForm());
stage.getIcons().add(image);

如果您正在使用 Linux,您可能会对 this question 感兴趣。

这是一个 MWE:

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        stage.getIcons().add(new Image(Main.class.getClassLoader().getResource("sample/icon.png").toExternalForm()));
        stage.setScene(new Scene(new Group(), 300, 275));
        stage.show();
    }

    public static void main(String[] args) {launch(args);}
}

这假定在目录 sample 中有一个图像 icon.png(与 Main class 相同的目录)。可以看到,图标既在应用程序的任务栏中,又在应用程序的标题栏中: