[英]getClass().getResource(..) makes image icon suddenly null
I have been reading a lot of questions related to this problem on this website.我在这个网站上阅读了很多与这个问题相关的问题。 But somehow, none of the suggested answers worked.但不知何故,建议的答案都没有奏效。
I want to use images in my java program.我想在我的 java 程序中使用图像。 I build the jar file today for the first time, but the images won't apply to the finished build.我今天第一次构建 jar 文件,但图像不适用于完成的构建。 I tried a lot.我尝试了很多。 getClass.getResource()
, the Class loader, reassigning my resource folder, moving the images, switching the images, different types of URLs, but I run into one of 2 problems either way: getClass.getResource()
,类加载器,重新分配我的资源文件夹,移动图像,切换图像,不同类型的 URL,但我遇到了两个问题之一:
I either get the above problem, when my code for the line is:我要么得到上述问题,当我的行代码是:
label.setIcon(new ImageIcon("URL/copied/from/directory.jpg"));
or, when I do something like this:或者,当我做这样的事情时:
label.setIcon(new ImageIcon(getClass().getResource("URL/copied/from/directory.jpg")));
Then I get a warning, which states this URL might be null.然后我收到一条警告,指出此 URL 可能为空。
when I use getClass().getResource.read("URL/copied/from/directory.jpg");
当我使用getClass().getResource.read("URL/copied/from/directory.jpg");
I actually get a compiling error with the same issue.我实际上遇到了同样问题的编译错误。
I am really not sure what is wrong with the code or the URL.我真的不确定代码或 URL 有什么问题。 One problem I have with the code examples in other similar questions is that I cannot tell which part of the code is syntax and which is not.我在其他类似问题中的代码示例中遇到的一个问题是,我无法分辨代码的哪一部分是语法,哪一部分不是。
As requested here is the original code:根据这里的要求是原始代码:
climateImageLabel.setIcon(new ImageIcon(getClass().getResource("src/DSA_Wildnistool/Pictures/KEIN BILD.jpg")));
label.setIcon(new ImageIcon("URL/copied/from/directory.jpg"));
String
should represent a File
rather than a URL
, but important point here, don't share with us 'something like' the code or String
being used.这行不通,因为String
应该代表File
而不是URL
,但这里的重点是,不要与我们分享“类似”正在使用的代码或String
的东西。 Share the actual copy/pasted text.共享实际复制/粘贴的文本。label.setIcon(new ImageIcon(getClass().getResource("URL/copied/from/directory.jpg")));
getResource(..)
to get an URL
( print out the URL ).使用getResource(..)
获取URL
(打印出 URL )。 Use the URL to form an Icon
, check it's not null
.使用 URL 形成一个Icon
,检查它不是null
。 Use the icon to create the label.使用图标创建标签。getClass().getResource.read("URL/copied/from/directory.jpg");
If you would like to try and continue with solving this problem, please post a short but complete code example based on the 2nd approach (the only one which made sense).如果您想尝试并继续解决此问题,请根据第二种方法(唯一有意义的方法)发布一个简短但完整的代码示例。
But to provide help, we'll need to know at least the package name of the class in which this code is called from (the 'short but complete example' will reveal that), and the directory structure of the project itself (at least showing the paths to the class and the image).但是为了提供帮助,我们至少需要知道调用此代码的类的包名称(“简短但完整的示例”将揭示这一点),以及项目本身的目录结构(至少显示类和图像的路径)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.