简体   繁体   English

如何使用getClass()。getResource()方法

[英]How to use getClass().getResource() method

When I create ImageIcon class objects I use the following code: 当我创建ImageIcon类对象时,我使用以下代码:

iconX = new ImageIcon (getClass().getResource("imageX.png"))

The above code works correctly either in an applet or a desktop app when the .png is in the same folder of the class. 当.png位于类的同一文件夹中时,上述代码可在applet或桌面应用程序中正常运行。

The question is: how to avoid a NullPointerException when the .Png is in another folder? 问题是:当.Png在另一个文件夹中时,如何避免NullPointerException Or how load the image in the object ImageIcon when it is in a different location to the class? 或者当ImageIcon位于与类不同的位置时,如何在ImageIcon加载图像?

I don't understand how this method works, if anyone can help me I appreciate it. 我不明白这种方法是如何工作的,如果有人能帮助我,我会很感激。 Thanks!! 谢谢!!

Take a look at this - Class#getResource(java.lang.String) 看看这个 - Class#getResource(java.lang.String)

Please click the link above and read the docs and follow to understand what's going on. 请点击上面的链接阅读文档,然后按照以了解发生了什么。

It says - 它说 -

If the name begins with a '/', then the absolute name of the resource is the portion of the name following the '/'. 如果名称以“/”开头,则资源的绝对名称是“/”后面的名称部分。

and

Otherwise, the absolute name is of the following form: 否则,绝对名称具有以下形式:

  modified_package_name/name 

Where the modified_package_name is the package name of this object with '/' substituted for '.'. 其中modified_pa​​ckage_name是此对象的包名称,其中'/'替换为'。'。

So, if this object (where you call getResource ) is in package /pkg1 ( / meaning pkg1 is right under the root of the classpath) and you used "imageX.png" then the result would be pkg1/imageX.png which is correct because that's where the image is located at. 所以,如果这个对象 (你调用getResource )在package /pkg1/意思是pkg1在classpath的根目录下)并且你使用了“imageX.png”那么结果就是pkg1/imageX.png这是正确的因为那是图像所在的位置。

But, if we moved the resource (imageX.png) to some other package /pkg2 and you called the method same way then the result would still be pkg1/imageX.png but this time it would be incorrect because the resource is actually located in /pkg2 . 但是,如果我们将资源(imageX.png)移动到其他某个包/pkg2并且您以同样的方式调用该方法,那么结果仍然是pkg1/imageX.png但这次它将是不正确的,因为资源实际位于/pkg2 That's when you end up with NPE. 那是你最后的NPE。

It's good to explicitly specify the full path of the resource starting from the root of the classpath. 从类路径的根开始明确指定资源的完整路径是很好的。 (eg "/pkg/imageX.png"). (例如“/pkg/imageX.png”)。

Hope this helps. 希望这可以帮助。

Simply supply the path to the resource. 只需提供资源的路径即可。

So, if you put the image in "/resources/images" within your Jar, you would simply use 因此,如果您将图像放在Jar中的“/ resources / images”中,您只需使用

iconX = new ImageIcon(getClass().getResource("/resources/images/imageX.png"))

Essentially what you're saying is, class loader, please search your class path for the following resource. 基本上你所说的是类加载器,请在类路径中搜索以下资源。

If the image is internal (you want a location relative to your project, or perhaps packaged into your jar), do what mad programmer said: 如果图像是内部的(你想要一个相对于你的项目的位置,或者可能打包到你的jar中),那么疯狂的程序员说:

iconX = new ImageIcon(getClass().getResource("/path/imageX.png"))

The path is relative, so path/ will be a folder in the same folder as your project (or packaged into your jar). 路径是相对的,因此path /将是与项目相同的文件夹中的文件夹(或打包到jar中)。

If you want an external image, simply hand ImageIcon constructor the path (ex. "C:/.../file.png"). 如果你想要一个外部图像,只需将ImageIcon构造函数放在路径上(例如“C:/.../ file.png”)。 This isn't recommended though, as it's better to use it as a resource. 但不建议这样做,因为最好将它用作资源。

For more info on the ImageIcon constructor, see here . 有关ImageIcon构造函数的更多信息,请参见此处 for more info on loading class resources, see here (Javadoc links) 有关加载类资源的更多信息,请参阅此处 (Javadoc链接)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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