[英]How do I change the default application icon in Java?
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.我正在使用 NetBeans,试图将熟悉的 Java 咖啡杯图标更改为一个 png 文件,该文件已保存在 jar 文件的资源目录中。 I've found many different web pages that claim they have a solution, but so far none of them work.我发现许多不同的网页声称他们有解决方案,但到目前为止它们都不起作用。
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.xyz package, if that makes any difference.包含此代码的类位于com.xyz包中,如果这有什么不同的话。 That class also extends JFrame.该类还扩展了 JFrame。 This code is throwing a MalformedUrlException on the first line.此代码在第一行抛出 MalformedUrlException。
Anyone have a solution that works?任何人都有有效的解决方案?
java.net.URL url = ClassLoader.getSystemResource("com/xyz/resources/camera.png");
路径前面可能需要也可能不需要“/”。
You can simply go Netbeans, in the design view, go to JFrame
property, choose icon image property, Choose Set Form's iconImage
property 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!在源代码中!
Try This write after试试这个写后
initcomponents();
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Your image address")));
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.这样,如果您将类移动到不同的包,则不必查找所有字符串,只需移动类及其资源目录即可。
/** 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 中使用的,它运行良好
在扩展javax.swing.JFrame
的类中,使用方法setIconImage
。
this.setIconImage(new ImageIcon(getClass().getResource("/resource/icon.png")).getImage());
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);
You can try this one , it works just fine :你可以试试这个,它工作得很好:
` ImageIcon icon = new ImageIcon(".//Ressources//User_50.png");
this.setIconImage(icon.getImage());`
inside frame constructor内部框架构造函数
try{
setIconImage(ImageIO.read(new File("./images/icon.png")));
}
catch (Exception ex){
//do something
}
And if I do not have a JFrame, since my application is a console application? 而且,如果我没有JFrame,那么由于我的应用程序是控制台应用程序? How can I put an icon? 如何放置图标?
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.