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


当前回答

你认为如何创建新的包,即图像。图标在你的SRC目录和移动到那里你。png图像?那么你只需要写:

Image image = new Image("/image/icons/nameOfImage.png");
primaryStage.getIcons().add(image);

这个解决方案非常适合我,但我仍然不确定它是否正确(这里是初学者)。

其他回答

我试过了,效果不错:

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

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

假设你的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(
      <yourclassname>.class.getResourceAsStream( "icon.png" ))); 

Icon.png和源文件在同一个文件夹下。

如果运行jar文件,Michael Berry指定的代码将改变标题栏和任务栏中的图标。快捷图标不可更改。

如果你运行一个用com编译的本地程序。zenjava,你必须添加一个链接到程序图标:

<plugin>
    <groupId>com.zenjava</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>8.8.3</version>
    <configuration>
    ...
        <bundleArguments>
            <icon>${project.basedir}/src/main/resources/images/filename.ico</icon>
        </bundleArguments>
    </configuration>
</plugin>

这将为快捷方式和任务栏添加一个图标。