简体   繁体   English

您将如何更改程序图标而不是默认的Java图标

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

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. 当我通过eclipse启动时,它会显示该图标,但是当我将其导出到Runnable Jar时,它将显示默认的Java图标,我不想使用Resource的方式进行操作,因为它甚至在IDE中也不起作用。

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

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. 并将您的图标文件放在src文件夹的res文件夹中。 But it'll display the icon of frame when you execute the jar file. 但是在执行jar文件时,它将显示框架图标。 Working fine for me. 对我来说很好。

Try this following code to set the iconImage to the frame: 尝试以下代码将iconImage设置为框架:

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

Even in this oracle doc you can find this same approach. 即使在此oracle文档中,您也可以找到相同的方法。

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. 看看这个explation了。

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. 我要感谢所有尝试提供帮助的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM