How would you change your programs Icon instead of the default java icon

≡放荡痞女 提交于 2019-12-25 03:44:32

问题


This shows the icon when I launch through eclipse, but when I export it to a Runnable Jar It shows the default Java icon, I do not want to use the Resource way of doing it since it doesn't work in the IDE even.

public static void main(String args[]) {
    Game component = new Game();
    ImageIcon img = new ImageIcon("res/game.png");

    JFrame frame = new JFrame();
    frame.add(component);
    frame.setTitle(NAME);
    frame.setIconImage(img.getImage());
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

    component.start();
}

回答1:


Try this following code to set the iconImage to the frame:

frame.setIconImage(new ImageIcon("res/game.png").getImage());

Even in this oracle doc you can find this same approach.

Edit: Have you tried this code. It might be helpful:

frame.setIconImage(new ImageIO.read(new File("res/game.png")));

Check out this explation too.




回答2:


please use the following code

Image image = ImageIO.read(getClass().getResourceAsStream("/res/icon.png"));
setIconImage(new ImageIcon(image).getImage());

and put your icon file in res folder of src folder. But it'll display the icon of frame when you execute the jar file. Working fine for me.




回答3:


The problem was that I was using the wrong run as file, a stupid mistake but thats what I get for not having my Workspace clean.

I'd like to thank everyone who tried to help.



来源:https://stackoverflow.com/questions/30137088/how-would-you-change-your-programs-icon-instead-of-the-default-java-icon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!