[英]ClassLoader.getResource() doesn't work for the icon files in a jar. The icons need to be present in the folder containing jar
I'm working on a Java program that uses icon files. 我正在使用图标文件的Java程序。 The icons are stored in a folder
Icons
in /src
. 图标存储在
/src
中的文件夹Icons
中。 The code to access an icon is 访问图标的代码是
image1 = new ImageIcon(Control.PrimaryGameFrame.class.getClassLoader().getResource("Icons/openFile.gif"));
image1 = new ImageIcon(Control.PrimaryGameFrame.class.getClassLoader()。getResource(“ Icons / openFile.gif”));;
The program runs properly in Eclipse. 该程序可以在Eclipse中正常运行。 But when I export its runnable jar to a folder
abc
, then it needs the Icons
folder to be present in abc
. 但是,当我将其可运行jar导出到文件夹
abc
,它需要将Icons
文件夹存在于abc
。 If I place the jar alone at a place, it doesn't load the icons. 如果我将罐子放在一个地方,它不会加载图标。 What changes are to be made to the code, for the jar to find the icon files in itself and not in the folder it is in?
要对代码进行什么更改,以使jar本身而不是在其所在的文件夹中查找图标文件?
Try this: 尝试这个:
ImageIcon image1 = new ImageIcon(getClass.getResource("Icons/openFile.gif");
This works fine for me. 这对我来说很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.