简体   繁体   English

getClass().getResource(..) 使图像图标突然为空

[英]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")));
  1. label.setIcon(new ImageIcon("URL/copied/from/directory.jpg"));
    This could not work, as the 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.共享实际复制/粘贴的文本。
  2. label.setIcon(new ImageIcon(getClass().getResource("URL/copied/from/directory.jpg")));
    This tries to do too much in one line.这试图在一行中做太多事情。 Break it down.打破它。 Use 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.使用图标创建标签。
  3. getClass().getResource.read("URL/copied/from/directory.jpg");
    Of course this would not compile, but the statement "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."当然,这不会编译,但声明“我在其他类似问题中的代码示例中遇到的一个问题是,我无法分辨代码的哪一部分是语法,哪一部分不是。” is especially telling.特别有说服力。 It is .. Java 101 to be able to read a compiler error and either solve it or form a specific question about solving it. Java 101 能够读取编译器错误并解决它或形成有关解决它的特定问题。 OTOH creating a cross-platform GUI using embedded resources is somewhat more advanced than Java 101. Perhaps you should set this project aside for a while and practice Java basics until you are familiar with them. OTOH 使用嵌入式资源创建跨平台 GUI 比 Java 101 高级一些。也许您应该将这个项目搁置一段时间并练习 Java 基础知识,直到您熟悉它们。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM