将图像添加到jar中 [英] Add images to jar

查看:99
本文介绍了将图像添加到jar中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图标设置为我的JFrame。我执行以下操作:

I want to set icon to my JFrame. I do the following:

Image icon = Toolkit.getDefaultToolkit().getImage("src/images/icon.jpg");
this.setIconImage(icon);

当我从netbeans运行此代码时它工作正常,但是当我尝试从jar运行此代码时文件,图像不会显示在我的JFrame中。我试图将图像作为资源加载:

It works fine when I run this code from netbeans, but when I try to run this code from jar file, images are not shown in my JFrame. I have tried to load images as resources:

this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/src/images/icon.jpg")));

但是当我运行此代码时,它失败并带有 NullPointerException

but when I run this code it fails with NullPointerException

 Uncaught error fetching image:
java.lang.NullPointerException
        at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:99)
        at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:113)
        at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240)
        at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
        at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)

我该怎么做?

编辑:
感谢所有,
通过将图像指定为

edit: Thanks to all, the problem was solved by specifying image as

Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("images/icon.JPG"))

至于它看起来很奇怪,如果它像是<会更好/ p>

As for it seems rather weird, and would be better if it was like

this.setIconImage(new ImageIcon(pathToIcon).getImage());


推荐答案

查看URLImageSource的源代码,看来 getConnection 抛出NPE的原因是它的url有 null 。这导致我怀疑

Looking at the source code of URLImageSource, it appears that the reason that getConnection throws an NPE is that it has a null for the url. And that leads me to suspect that

getClass().getResource("/src/images/icon.jpg")

返回 null 。如果它找不到具有该路径名的资源,它就会这样做。

is returning null. It would do that if it could not locate a resource with that pathname.

我敢打赌问题是你的路径错了。

I bet that the problem is that you've got the path wrong.

要证明/反驳这一点,你应该在JAR文件上运行 jar tvf ,并查找匹配<$ c $的行C> icon.jpg。相应的路径名是否与您使用的路径名相同?如果没有,请使用 getResource 调用中匹配行的路径名,它应该可以工作。或者,如果文件根本没有显示,请查看NetBeans构建配置,告诉它将什么放入JAR文件中。 (我不是NetBeans用户,所以我不能说你需要在哪里看...)

To prove / disprove this, you should run jar tvf on the JAR file, and look for the line that matches "icon.jpg". Is the corresponding pathname the same as what you are using? If not, use the pathname from the matching line in the getResource call and it should work. Alternatively, if the file doesn't show up at all, look at the NetBeans build configs that tell it what to put in the JAR file. (I'm not a NetBeans user, so I can't say where you would need to look ...)

如果这导致你无处可去,另一种可能性是 getClass()。getResource(...)正在使用一个不知道包含该JAR文件的JAR文件的类加载器图片。 (这对我来说似乎不太可能......)

If that leads you absolutely nowhere, another possibility is that getClass().getResource(...) is using a classloader that doesn't know about the JAR file containing the image. (This seems pretty improbable to me ...)

这篇关于将图像添加到jar中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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