如何更改Java中的默认应用程序图标? [英] How do I change the default application icon in Java?
问题描述
我正在使用NetBeans,尝试将熟悉的Java咖啡杯图标更改为我保存在jar文件的资源目录中的png文件。我发现许多不同的网页声称他们有解决方案,但到目前为止它们都没有工作。
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.
这是我现在所拥有的(省略了尝试) -catch block):
Here's what I have at the moment (leaving out the try-catch block):
URL url = new URL("com/xyz/resources/camera.png");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
getFrame().setIconImage(img);
包含此代码的类位于 com.xyz 包中,如果这有任何区别。该类还扩展了JFrame。此代码在第一行抛出MalformedUrlException。
The class that contains this code is in the com.xyz package, if that makes any difference. That class also extends JFrame. This code is throwing a MalformedUrlException on the first line.
任何人都有一个有效的解决方案吗?
Anyone have a solution that works?
推荐答案
java.net.URL url = ClassLoader.getSystemResource("com/xyz/resources/camera.png");
路径前面可能需要或者不需要'/'。
May or may not require a '/' at the front of the path.
这篇关于如何更改Java中的默认应用程序图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!