打包包含图像的程序 [英] Packaging a program containing images

查看:104
本文介绍了打包包含图像的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多问题打包我的java程序,其中包含图像到jar中以便转换成可执行文件。图像已用于程序和按钮的背景中。请参阅下图,其中显示了我希望转换为jar的程序。

I'm having massive issues packaging my java program which contains images into a jar for conversion into and executable file. The images have been used in the background of the program and buttons. Please see the diagram below which shows the program I desire to convert to a jar.

图片

如上所示,程序运行正常。我创建了相同的程序,没有自定义背景和自定义按钮,不包含任何图像,我成功将其打包到jar中,然后打包成.exe文件。

As you see above the program runs OK. I created the same program with no custom background and custom buttons containing no images and I successfully packaged it into a jar and subsequently into an .exe file.

关于绘图我的背景我这样做如下:

With regards to drawing my background I'm doing this as follows:

public void paintComponent(Graphics g) {
    Image img = new ImageIcon("imgs/Bgnd1.jpg").getImage();
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);
    g.drawImage(img, 0, 0, null);
} 

关于用图像创建我的4个自定义按钮,我正在做以下:

With regards to creating my 4 custom buttons with images, I'm doing the following:

// Prepare rollover images
ImageIcon F1 = new ImageIcon("imgs/btn_f1_not_selected.jpg");
ImageIcon F1rollOver = new ImageIcon("imgs/btn_f1_selected.jpg");

// Create F1 button
final JButton btnF1 = new JButton(F1);
//btnF1.setOpaque(false);
btnF1.setContentAreaFilled(false);
btnF1.setBorder(null);
btnF1.setBorderPainted(false);
btnF1.setFocusPainted(false);
btnF1.setRolloverIcon(F1rollOver);

我尝试将图像放在bin文件夹中并创建背景我改变了上面的方法关于声明/获取图像。

I attempted placing the images in the bin folder and for the creation of the background I altered the above method with regards to the declaration/fetching of the image.

public void paintComponent(Graphics g) {
        String path = "Bgnd11.jpg";
        java.net.URL imgURL = getClass().getResource(path);     
        Image img = new ImageIcon(imgURL).getImage();
        Dimension size = new Dimension(img.getWidth(observer), img.getHeight(observer));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
        g.drawImage(img, 0, 0, null);
}

我还试图获取创建按钮所需的图像,如下所示然后将它们传递给我的按钮,但这不起作用。

I also attempted fetching the images needed for the creation of my buttons as indicated below and then passing them to my button but this did not work.

String path = "Bgnd11.jpg";
java.net.URL imgURL = getClass().getResource(path);     
Image img = new ImageIcon(imgURL).getImage();

如何定位&加载图像?

How to locate & load the images?

推荐答案

在第一次尝试中,您将从当前目录中的文件系统加载图像,是启动 java javaw 命令的目录。这就是阻止您将图像与应用程序捆绑在一起的原因。显然,你的应用程序的最终用户不会在当前目录中拥有这些图像,并且他当前的目录将根据他启动应用程序的方式而改变。

In your first attempt, you're loading images from the file system, in the current directory, which is the directory from which the java of javaw command is started. That's what prevents you from bundling the images with your applications. Obviously, the end user of your app won't have the images in his current directory, and his current directory will change depending on how he launches the application.

你应该而是将图像打包在jar文件中,因此存在于类路径中,因此在第二次尝试时使用ClassLoader加载它们。

You should instead have the images packaged inside the jar file, and thus be present in the classpath, and thus load them using the ClassLoader as you're doing in your second attempt.

假设它们位于jar的 / resources / images 文件夹中,因此对应于包 resources.images

Let's say they're in the folder /resources/images of the jar, which thus corresponds to the package resources.images.

使用 getClass()。getResource(Bgnd11.jpg),正如javadoc所示,试图找到 Bgnd11.jpg getClass()返回的类相同的包中。因此,如果类在包 resources.images 中,它将在我们的示例中起作用。如果不是,则应使用资源的绝对路径:

Using getClass().getResource("Bgnd11.jpg"), as the javadoc indicates, tries to find Bgnd11.jpg in the same package as the class returned by getClass(). So, it would work in our example if the class was in the package resources.images. If it's not, you should use the absolute path of the resource:

URL imgURL = getClass().getResource("/resources/images/Bgnd11.jpg");   

另外,不要弄乱bin文件夹。这是Eclipse的目标文件夹,执行干净的构建将删除此目录中的所有内容。只需将图像添加到源目录中的相应包中,Eclipse将在构建项目时自动将它们复制到目标目录。

Also, don't mess with the bin folder. This is the destination folder of Eclipse, and doing a clean build will remove everything from this directory. Just add the images to the appropriate package in the source directory, and Eclipse will automatically copy them to the destination directory when building the project.

这篇关于打包包含图像的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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