问题
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