How would you change your programs Icon instead of

2019-09-16 08:36发布

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();
}

3条回答
太酷不给撩
2楼-- · 2019-09-16 08:54

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.

查看更多
姐就是有狂的资本
3楼-- · 2019-09-16 08:59

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.

查看更多
SAY GOODBYE
4楼-- · 2019-09-16 09:01

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.

查看更多
登录 后发表回答