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


当前回答

我在我的应用程序中使用了这个

Image icon = new Image(getClass().getResourceAsStream("icon.png"));
window.getIcons().add(icon);

这个窗口就是舞台。

其他回答

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

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")));

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

我试过了,效果很好。代码是:

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

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

stage.getIcons().add(new Image("/images/logo_only.png"));

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

在JavaFX的标题栏中插入自己的图标的另一种简单方法是使用以下方法将图像添加到你的主阶段:

Image ico = new Image("resources/images/iconLogo.png");
stage.getIcons().add(ico);

确保你导入javafx.scene.image.Image(如果使用netbeans这样的ide,这应该会自动为你完成)。