如何更改 Java 中的默认应用程序图标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/209812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:24:07  来源:igfitidea点击:

How do I change the default application icon in Java?

javaicons

提问by Bill the Lizard

I'm using NetBeans, trying to change the familiar Java coffee cup icon to a png file that I have saved in a resources directory in the jar file. I've found many different web pages that claim they have a solution, but so far none of them work.

我正在使用 NetBeans,试图将熟悉的 Java 咖啡杯图标更改为一个 png 文件,该文件已保存在 jar 文件的资源目录中。我发现许多不同的网页声称他们有解决方案,但到目前为止它们都不起作用。

Here's what I have at the moment (leaving out the try-catch block):

这是我目前所拥有的(不包括 try-catch 块):

URL url = new URL("com/xyz/resources/camera.png");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
getFrame().setIconImage(img);

The class that contains this code is in the com.xyzpackage, if that makes any difference. That class also extends JFrame. This code is throwing a MalformedUrlException on the first line.

包含此代码的类位于com.xyz包中(如果这有什么不同的话)。该类还扩展了 JFrame。此代码在第一行抛出 MalformedUrlException。

Anyone have a solution that works?

任何人都有有效的解决方案?

采纳答案by JeeBee

java.net.URL url = ClassLoader.getSystemResource("com/xyz/resources/camera.png");

May or may not require a '/' at the front of the path.

路径前面可能需要也可能不需要“/”。

回答by John Gardner

Or place the image in a location relative to a class and you don't need all that package/path info in the string itself.

或者将图像放在相对于类的位置,并且您不需要字符串本身中的所有包/路径信息。

com.xyz.SomeClassInThisPackage.class.getResource( "resources/camera.png" );

That way if you move the class to a different package, you dont have to find all the strings, you just move the class and its resources directory.

这样,如果您将类移动到不同的包,则不必查找所有字符串,只需移动类及其资源目录即可。

回答by user1456935

    /** Creates new form Java Program1*/
    public Java Program1() 


    Image im = null;
    try {
    im = ImageIO.read(getClass().getResource("/image location"));
    } catch (IOException ex) {
    Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex);
    }
    setIconImage(im);

This is what I used in the GUI in netbeans and it worked perfectly

这是我在 netbeans 的 GUI 中使用的,它运行良好

回答by Ayoub Aneddame

You can simply go Netbeans, in the design view, go to JFrameproperty, choose icon image property, Choose Set Form's iconImageproperty using: "Custom code" and then in the Form.SetIconImage()function put the following code:

您可以简单地转到 Netbeans,在设计视图中,转到JFrame属性,选择图标图像属性,iconImage使用:“自定义代码”选择设置表单的属性,然后在Form.SetIconImage()函数中放入以下代码:

Toolkit.getDefaultToolkit().getImage(name_of_your_JFrame.class.getResource("image.png"))

Do not forget to import:

不要忘记导入:

import java.awt.Toolkit;

in the source code!

在源代码中!

回答by user2601995

In a class that extends a javax.swing.JFrameuse method setIconImage.

在扩展javax.swing.JFrame使用方法的类中setIconImage

this.setIconImage(new ImageIcon(getClass().getResource("/resource/icon.png")).getImage());

回答by user2895893

Try This write after

试试这个写后

initcomponents();

setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Your image address")));

回答by Rrezart A. Prebreza

Example:

例子:

URL imageURL = this.getClass().getClassLoader().getResource("Gui/icon/report-go-icon.png");
ImageIcon iChing = new ImageIcon("C:\Users\RrezartP\Documents\NetBeansProjects\Inventari\src\Gui\icon\report-go-icon.png");      
btnReport.setIcon(iChing); 
System.out.println(imageURL);

回答by ron190

You should define icons of various size, Windows and Linux distros like Ubuntu use different icons in Taskbar and Alt-Tab.

您应该定义各种大小的图标,Windows 和 Linux 发行版(如 Ubuntu)在任务栏和 Alt-Tab 中使用不同的图标。

public static final URL ICON16 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug16.png");
public static final URL ICON32 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug32.png");
public static final URL ICON96 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug96.png");

List<Image> images = new ArrayList<>();
try {
    images.add(ImageIO.read(HelperUi.ICON96));
    images.add(ImageIO.read(HelperUi.ICON32));
    images.add(ImageIO.read(HelperUi.ICON16));
} catch (IOException e) {
    LOGGER.error(e, e);
}

// Define a small and large app icon
this.setIconImages(images);

回答by Alex S

inside frame constructor

内部框架构造函数

try{    
       setIconImage(ImageIO.read(new File("./images/icon.png")));   
   }
catch (Exception ex){
       //do something
   }

回答by Mohamed Aharrat

You can try this one, it works just fine :

你可以试试这个,它工作得很好:

`   ImageIcon icon = new ImageIcon(".//Ressources//User_50.png");
    this.setIconImage(icon.getImage());`