是否可以使用JavaFX更改应用程序图标,还是必须使用Swing?


当前回答

我试过了,效果不错:

stage.getIcons().add(new Image(getClass().getResourceAsStream("../images/icon.png")));

其他回答

stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("/icon.png")));

如果你的icon.png在资源目录下,记得在前面加上一个“/”,否则它将不起作用

我试过了,效果不错:

stage.getIcons().add(new Image(getClass().getResourceAsStream("../images/icon.png")));

您可以在fxml中添加它。阶段水平

<icons>
    <Image url="@../../../my_icon.png"/>
</icons>

假设你的stage是“stage”,文件在文件系统上:

stage.getIcons().add(new Image("file:icon.png"));

根据下面的评论,如果它被包装在一个容器中,你将需要使用以下方法:

stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("icon.png")));
stage.getIcons().add(new Image("/images/logo_only.png"));

在src文件夹中创建图像文件夹并从中获取图像是一个好习惯。