简体   繁体   English

Java Swing:显示 Jar 中的图像

[英]Java Swing: Displaying images from within a Jar

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?有没有办法在运行时从 jar 中提取图像,以便我可以打开它? Or, is there a better way to do this?或者,有没有更好的方法来做到这一点?

I'd like to distribute a single jar file if possible.如果可能,我想分发一个 jar 文件。

To create an ImageIcon from an image file within the same jars your code is loaded:要从加载代码的相同 jar 中的图像文件创建ImageIcon

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

Class.getResource returns a URL of a resource (or null !). Class.getResource返回资源的 URL(或null !)。 ImageIcon has a constructors that load from a URL. 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的文档。

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我的JAR文件
- com (class files in here) - com(这里的类文件)
- images - 图片
----image.jpg ----图片.jpg

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。

In netbeans 8.1 what I've done is to include the folder of icons and other images called Resources inside the src folder in the project file.在 netbeans 8.1 中,我所做的是在项目文件的src文件夹中包含名为Resources的图标和其他图像文件夹。 So whenever i build Jar file the folder is included there.The file tree should be like this:所以每当我构建Jar文件时,文件夹都包含在那里。文件树应该是这样的:

  • src (Java files in source packges are here) src(源包中的Java文件在这里)
  • ** PACKAGE YOU NAMED IN PROJECT** ** 您在项目中命名的包**

    • file.java文件.java
  • Resources资源

    • image.jpg图片.jpg

The code should be like:代码应该是这样的:

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

Load image in from Jar file during run time is the same as loading image when executed from IDE eg netbeans the difference is that when loading image from JAR file the path must be correct and its case sensitive (very important).在运行时从 Jar 文件加载图像与从 IDE(例如 netbeans)执行时加载图像相同,区别在于从 JAR 文件加载图像时,路径必须正确且区分大小写(非常重要)。 This works for me这对我有用

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.如果 "/Pictures/firstgame/habitat1.jpg" 中的 p 是小写,它就不会工作。 check spaces, cases and spelling检查空格、大小写和拼写

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

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