问题
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:
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.
来源:https://stackoverflow.com/questions/27017031/javafx-2-window-icon-not-working