使用我的JAR文件导出图像 [英] Exporting Images with my JAR file

查看:153
本文介绍了使用我的JAR文件导出图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Eclipse,我希望将img源文件夹中的一些图像与我的.jar一起导出,以便它们显示在JAR中。

I am using Eclipse, and I wish to export some images in the "img" source folder along with my .jar, so they show up in the JAR.

我的Heirarchy是:

My Heirarchy is :


Project> src> Package> FILE.java

Project>src>Package>FILE.java

然后在项目中另一个源文件夹叫做:

Then in project is another source folder called :


img> IMAGE.png

img>IMAGE.png

我目前使用以下方式链接到图像:

I am currently linking to the images using :

lblNewLabel_1.setIcon(new ImageIcon("img/logo1.png"));

这一切正常,直到我导出它。

And that works fine, until I export it.

我将其导出为Runnable JAR文件,其中提取所需的库已勾选,因为中间的将不允许打开.jar。 Extract允许它打开,但图像不显示。

I am exporting it as an Runnable JAR file with Extract required libraries ticked, as the middle one will not allow the .jar to open. Extract allows it to open, but the images do not show.

推荐答案

在JAR中加载数据时,路径不一样。

Path are not the same when you load data inside a JAR.

从JAR运行时,您可以使用此方法加载图像:

You can use this method to load image when you run from a JAR:

/**
  * Create an instance of ImageIcon with the given path
  * @param path  String - path of the image
  * @return  ImageIcon - ImageIcon made with the image at the given path
  */
private ImageIcon createImageIcon(String path) {
    if (path != null) {
        URL tmp = getClass().getResource(path.replace("\\", "/"));
        if(tmp!=null)
            return new ImageIcon(tmp);
        else
            return new ImageIcon();
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

这篇关于使用我的JAR文件导出图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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