Java Swing:显示 Jar 中的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:22:32  来源:igfitidea点击:

Java Swing: Displaying images from within a Jar

javaswingembedded-resourceimageicon

提问by jjnguy

When running a Java app from eclipse my ImageIcon shows up just fine.

从 Eclipse 运行 Java 应用程序时,我的 ImageIcon 显示正常。

But after creating a jar the path to the image obviously gets screwed up.

但是在创建一个 jar 之后,图像的路径显然被搞砸了。

Is there a way to extract an image from the jar at runtime so I can then open it up? Or, is there a better way to do this?

有没有办法在运行时从 jar 中提取图像,以便我可以打开它?或者,有没有更好的方法来做到这一点?

I'd like to distribute a single jar file if possible.

如果可能,我想分发一个 jar 文件。

采纳答案by Tom Hawtin - tackline

To create an ImageIconfrom an image file within the same jars your code is loaded:

要从ImageIcon加载代码的相同 jar 中的图像文件创建一个:

new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg"))

Class.getResourcereturns a URL of a resource (or null!). ImageIconhas a constructors that load from a URL.

Class.getResource返回资源的 URL(或null!)。ImageIcon有一个从 URL 加载的构造函数。

To construct a URL for a resource in a jar not on your "classpath", see the documentation for java.net.JarURLConnection.

要为不在“类路径”中的 jar 中的资源构建 URL,请参阅java.net.JarURLConnection.

回答by Outlaw Programmer

You can try something like:

您可以尝试以下操作:

InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");

In your JAR file, you might have a directory structure of:

在您的 JAR 文件中,您可能具有以下目录结构:

MyJAR.jar
- com (class files in here)
- images
----image.jpg

MyJAR.jar
- com(这里的类文件)
- 图像
----image.jpg

回答by ekerner

This is working for me to load and set the content pane background image:

这对我来说可以加载和设置内容窗格背景图像:

jar (or build path) contains:

jar(或构建路径)包含:

 - com
 - img
 ---- bg.png

java contains:

java包含:

JFrame f = new JFrame("Testing load resource from jar");
try {
    BufferedImage bg = ImageIO.read(getClass().getResource("/img/bg.png"));
    f.setContentPane(new ImagePanel(bg));
} catch (IOException e) {
    e.printStackTrace();
}

Tested and working in both jar and unjarred (is that the technical term) execution.

在 jar 和 unjarred(这是技术术语)执行中测试和工作。

BTW getClass().getClassLoader().getResourceAsStream("/img/bg.png")- which I tried first - returned me a null InputStream.

顺便说一句getClass().getClassLoader().getResourceAsStream("/img/bg.png")- 我首先尝试过 - 返回了一个空的 InputStream。

回答by Tobore Igbe

Load image in from Jar file during run time is the same as loading image when executed from IDE e.g netbeans the difference is that when loading image from JAR file the path must be correct and its case sensitive(very important). This works for me

在运行时从 Jar 文件加载图像与从 IDE(例如 netbeans)执行时加载图像相同,区别在于从 JAR 文件加载图像时,路径必须正确且区分大小写(非常重要)。这对我有用

 image1 = new javax.swing.ImageIcon(getClass().getResource("/Pictures/firstgame/habitat1.jpg"));
        img = image1.getImage().getScaledInstance(lblhabitat1.getWidth(), lblhabitat1.getHeight(), Image.SCALE_SMOOTH);
        lblhabitat1.setIcon(new ImageIcon(img));

if p in "/Pictures/firstgame/habitat1.jpg" is in lower case it wont work. check spaces, cases and spelling

如果 "/Pictures/firstgame/habitat1.jpg" 中的 p 是小写,它就不会工作。检查空格、大小写和拼写

回答by Abdelsalam Shahlol

In netbeans 8.1 what I've done is to include the folder of icons and other images called Resourcesinside the srcfolder in the project file. So whenever i build Jarfile the folder is included there.The file tree should be like this:

在 netbeans 8.1 中,我所做的是在项目文件的src文件夹中包含名为Resources的图标和其他图像文件夹。所以每当我构建Jar文件时,文件夹都包含在那里。文件树应该是这样的:

  • src (Java files in source packges are here)
  • ** PACKAGE YOU NAMED IN PROJECT**

    • file.java
  • Resources

    • image.jpg
  • src(源包中的Java文件在这里)
  • ** 您在项目中命名的包**

    • 文件.java
  • 资源

    • 图片.jpg

The code should be like:

代码应该是这样的:

jToggleButton1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/resources/image.jpg")));