使用 getClass().getResource() 加载资源 [英] Loading resources using getClass().getResource()

查看:48
本文介绍了使用 getClass().getResource() 加载资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载图像以用作应用程序中的图标.根据此教程,适当的方法是:>

I am trying to load an image to use as an icon in my application. The appropriate method according to this tutorial is:

protected ImageIcon createImageIcon(String path, String description) 
{
    java.net.URL imgURL = getClass().getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

因此,我放置了文件的位置,并将其作为参数传递给此函数.这不起作用,即 imgURL 为空.当我尝试通过显式传入路径来创建 ImageIcon 时:

So, I placed the location of the file, and passed it as a parameter to this function. This didn't work, i.e. imgURL was null. When I tried creating the ImageIcon by passing in the path explicitly:

ImageIcon icon  = new ImageIcon(path,"My Icon Image");

效果很好!因此,应用程序可以从明确定义的路径中选取图像,但不会使用 getResources() 选取图像.在这两种情况下,路径变量的值是相同的.为什么它不起作用?类加载器如何找到资源?

It worked great! So the application can pick up the image from an explicitly defined path, but didn't pick up the image using getResources(). In both cases, the value of the path variable is the same. Why wouldn't it work? How are resources found by the class loader?

谢谢.

推荐答案

您可以以这种格式请求路径:

You can request a path in this format:

/package/path/to/the/resource.ext

即使是在内存中创建类的字节也是这样找到的:

Even the bytes for creating the classes in memory are found this way:

my.Class -> /my/Class.class

getResource 会给你一个 URL,可用于检索 InputStream.

and getResource will give you a URL which can be used to retrieve an InputStream.

但是...我建议直接使用 getClass().getResourceAsStream(...) 和相同的参数,因为它直接返回 InputStream 而不必担心创建一个(可能是复杂的)URL 对象,它必须知道如何创建 InputStream.

But... I'd recommend using directly getClass().getResourceAsStream(...) with the same argument, because it returns directly the InputStream and don't have to worry about creating a (probably complex) URL object that has to know how to create the InputStream.

简而言之:尝试使用 getResourceAsStream 和一些使用 InputStream 作为参数的 ImageIcon 构造函数.

In short: try using getResourceAsStream and some constructor of ImageIcon that uses an InputStream as an argument.

类加载器

如果您的应用有很多类加载器,请务必小心.如果您有一个简单的独立应用程序(没有服务器或复杂的东西),您不必担心.如果 ImageIcon 能够找到它,我认为情况并非如此.

Be careful if your app has many classloaders. If you have a simple standalone application (no servers or complex things) you shouldn't worry. I don't think it's the case provided ImageIcon was capable of finding it.

类路径

getResource 是——正如 mattb 所说——用于从类路径(从你的 .jar 或类路径目录)加载资源.如果您要捆绑一个应用程序,那么完全拥有它会很好,因此您可以将图标文件包含在您应用程序的 jar 中并通过这种方式获取它.

getResource is—as mattb says—for loading resources from the classpath (from your .jar or classpath directory). If you are bundling an app it's nice to have altogether, so you could include the icon file inside the jar of your app and obtain it this way.

这篇关于使用 getClass().getResource() 加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆