Java 在 Jar 文件中设置图标图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2483283/
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
Set Icon Image in Jar file
提问by Mr CooL
The following code works fine when running on NetBeans.
以下代码在 NetBeans 上运行时运行良好。
this.getFrame().setIconImage(Toolkit.getDefaultToolkit().getImage("PlagiaLyzerIcon.png"));
However, once it was built into Jar file, the icon was gone.
但是,一旦将其内置到 Jar 文件中,该图标就消失了。
Anyone has idea what's the problem? I realized I've to put the icon image on the root directory, however, after compiling into JAR, the icon gone.
任何人都知道有什么问题吗?我意识到我必须将图标图像放在根目录中,但是,在编译成 JAR 后,图标消失了。
Thanks for any help ...
谢谢你的帮助 ...
Hi everyone, the problem was solved with the following code,
大家好,问题已通过以下代码解决,
this.getFrame().setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("plagialyzer/resources/PlagiaLyzerIcon.png")));
It works once compiled into jar file.
一旦编译成 jar 文件,它就可以工作。
采纳答案by hecvd
Use
用
this.getFrame().setIconImage(
new imageIcon(getClass().getClassLoader().getResource("PlagiaLyzerIcon.png"))
);
instead.
反而。
Note:
笔记:
this line only works if the images are in the root of the jar file. If not, you have to specify the folder on the string:
此行仅在图像位于 jar 文件的根目录中时才有效。如果没有,您必须在字符串上指定文件夹:
getResource("yourfolder/PlagiaLyzerIcon.png")
回答by Amokrane Chentir
Did you specify the build path to your icons in the options of Netbean before exporting the JAR? On Eclipse it's done by adding a source folder inside the Java Build Path as shown in this screenshot. Should be the same way on Netbeans?
在导出 JAR 之前,您是否在 Netbean 的选项中指定了图标的构建路径?在Eclipse上它是通过将Java构建路径内的源文件夹如本做截图。在Netbeans上应该是一样的吗?
回答by Pindatjuh
That is because the Netbeans IDE has a different classpath, than when running the jar-file stand-alone (without Ant).
这是因为 Netbeans IDE 的类路径与独立运行 jar 文件(没有 Ant)时的类路径不同。
Assume your Netbeans project is at location /project/
:
假设您的 Netbeans 项目位于以下位置/project/
:
The classpath is: /project/build/classes/
and the project root /project/
. If your icons are stored in: /project/myicons/
, then they are part of the classpath, since /project/
is too. But when you build your project, only files in /project/build/classes/
will eventually end up in the jar-file, these files are "build" from /projcet/src/
.
类路径是:/project/build/classes/
和项目 root /project/
。如果您的图标存储在: 中/project/myicons/
,那么它们就是类路径的一部分,因为/project/
也是。但是当您构建项目时,只有其中的文件/project/build/classes/
最终会出现在 jar 文件中,这些文件是从/projcet/src/
.
Solution:
解决方案:
Move your icons into a source-package: /project/src/myicons/
将您的图标移动到源包中: /project/src/myicons/
Or, add the /project/myicons/
folder to your sources (right-click your project -> Properties -> Sources -> add your folder there)
或者,将/project/myicons/
文件夹添加到您的源(右键单击您的项目 -> 属性 -> 源 -> 在那里添加您的文件夹)
回答by Gerson Castellanos
Thanks a lot...
非常感谢...
Here the code that works for me!!!!
这是对我有用的代码!!!!
BufferedImage image = null;
try {
image = ImageIO.read(getClass().getClassLoader().getResource("images/frame.png"));
} catch (IOException e) {
e.printStackTrace();
}
super.setIconImage(image);
回答by Irene Maryanne
Using NetBeans like myself, just put the logo (in my case mylogo.png) inside src/ directory in your project folder. Make sure size is 32 x 32 pixel to ensure best quality for icon. I couldn't manage to set .ico images as my JFrame icon but .png worked well. By extending JFrame, I directly set my jar icon with:
像我一样使用 NetBeans,只需将徽标(在我的示例中为 mylogo.png)放在项目文件夹的 src/ 目录中。确保大小为 32 x 32 像素以确保图标的最佳质量。我无法将 .ico 图像设置为我的 JFrame 图标,但 .png 运行良好。通过扩展 JFrame,我直接设置了我的 jar 图标:
Image getIcon()
{ return Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("mylogo.png")); }
setIconImage(getIcon());
After clean and build jar, the jar file icon still isn't changed (still the jar default icon) but when you execute the jar file the icon in title bar and taskbar will be your custom icon. You can change the jar file icon by creating shortcuts and change icon for that shortcut. Best resolution for shortcut icon is 512 x 512 pixel .ico
在清理和构建 jar 之后,jar 文件图标仍然没有改变(仍然是 jar 默认图标)但是当您执行 jar 文件时,标题栏和任务栏中的图标将是您的自定义图标。您可以通过创建快捷方式并更改该快捷方式的图标来更改 jar 文件图标。快捷方式图标的最佳分辨率是 512 x 512 像素 .ico