[英]Can't get images working, with getResource()
I'm trying to make a jar, but the program needs images. 我试图做一个罐子,但是程序需要图像。 When I ran the jar, the images didn't show up.
当我运行广口瓶时,图像没有显示。 However, they did in eclipse.
但是,他们确实在日食中。 I used this code:
我使用以下代码:
label.setIcon(new ImageIcon("res/img/icon.png"));
Then I went looking on the internet for a way to fix it. 然后我去互联网上寻找解决方法。 I found this question here on StackOverflow, but when i tried it in my code, it's throwing a
NullPointerException
(also in eclipse). 我在StackOverflow上找到了这个问题,但是当我在代码中尝试该问题时,它抛出了
NullPointerException
(也在eclipse中)。 This is my code now: 现在是我的代码:
label.setIcon(new ImageIcon(getClass().getResource("/res/img/icon.png")));
The images are in a separate folder in the package, called "res": 图像位于程序包中的单独文件夹中,称为“ res”:
make sure you typed image names correctly as well 确保您也正确键入了图像名称
That is the best way of dealing with images 那是处理图像的最好方法
update 更新
try 尝试
label.setIcon(new ImageIcon(getClass().getResource("res/img/icon.png")));
instead of 代替
label.setIcon(new ImageIcon(getClass().getResource("/res/img/icon.png")));
Have a look that your RES
folder is actually within the eclipse SRC
folder. 看一下您的
RES
文件夹是否确实在Eclipse SRC
文件夹中。 Sometimes, users add a folder to the eclipse project instead. 有时,用户会将文件夹添加到eclipse项目中。
Also: 也:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.