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


当前回答

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

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

其他回答

如果你得到无效的URL或资源没有找到,把你的图标.png在“bin”文件夹在你的工作空间。

使用这段代码行,您可以轻松地将图标放到应用程序中

stage.getIcons().add(new Image(“image path”) );

如果你有一个图像文件夹并且图标保存在其中,使用这个

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

如果你直接从你的包中使用它,这不是一个好的做法,使用这个

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

如果你有一个文件夹结构里面有你的图标

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

图像文件夹需要在资源文件夹。

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

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

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

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