JavaFX 2 Window Icon not working [closed]

2019-06-02 18:25发布

I'm trying to add an Icon to my JavaFX 2 application, but the ways I have found don't seem to work.

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

The icon is 32x32 in size.

When I try

Image icon = new Image("http://goo.gl/kYEQl");

It does work, in Netbeans and in the runnable jar.

I hope this can be fixed.

1条回答
地球回转人心会变
2楼-- · 2019-06-02 18:43

The problem was in the icon itself. It did load it like it should, but for some reason it didn't display like it should.

I remade the icon I was trying to use to different sizes (16x16 up to 512x512) and added them all to the icon list.

stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_16.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_32.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_64.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_128.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_256.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_512.png")));

Now it uses the icon like it should.

查看更多
登录 后发表回答