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


当前回答

假设你的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(“image path”) );

我试过了,效果不错:

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

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

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

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

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

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

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

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

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

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