在运行作为 JAR 存档分发的项目时加载图像等资源 [英] Loading resources like images while running project distributed as JAR archive

查看:17
本文介绍了在运行作为 JAR 存档分发的项目时加载图像等资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 GUI 出现错误.尝试设置标题栏图标,然后将其包含在 Runnable JAR 中.

I am having a error for my GUI. Trying to set title bar icon then be included in a Runnable JAR.

BufferedImage image = null;
try {
    image = ImageIO.read(getClass().getClassLoader().getResource("resources/icon.gif"));
} 
catch (IOException e) {
    e.printStackTrace();
}

frame.setIconImage(image);

这是我得到的错误:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at GUI.<init>(GUI.java:39)
    at GUI.main(GUI.java:351)

图像位于正确的目录中,其中resources"文件夹是根目录项目文件

The image is in the correct directory which "resources" folder is the root of the project file

推荐答案

首先改变这一行:

image = ImageIO.read(getClass().getClassLoader().getResource("resources/icon.gif"));

到这个:

image = ImageIO.read(getClass().getResource("/resources/icon.gif"));

更多信息,关于这两种方法之间的区别在哪里,可以在这个线程上找到 - 加载资源的不同方式

More info, on as to where lies the difference between the two approaches, can be found on this thread - Different ways of loading a Resource

对于 Eclipse:

对于 NetBeans:

对于 IntelliJ IDEA:

  • 右键单击项目的 src 文件夹.选择新建 -> 包
  • 新包对话框下,输入包的名称,例如resources.点击确定
  • 右键点击资源包.选择新建 -> 包
  • 新包对话框下,输入包的名称,例如images.点击确定
  • 现在选择要添加到项目中的图像,复制它.IDE 中右键单击 resources.images 包,然后选择粘贴
  • 使用最后一个链接检查现在如何在 Java 代码中访问此文件.虽然对于这个例子,我们会使用

  • Right-Click the src Folder of the Project. Select New -> Package
  • Under New Package Dialog, type name of the package, say resources. Click OK
  • Right Click resources package. Select New -> Package
  • Under New Package Dialog, type name of the package, say images. Click OK
  • Now select the image that you want to add to the project, copy it. Right click resources.images package, inside the IDE, and select Paste
  • Use the last link to check how to access this file now in Java code. Though for this example, one would be using

getClass().getResource("/resources/images/myImage.imageExtension");

Shift + F10,创建并运行项目.资源和图像文件夹将在out文件夹内自动创建.

Press Shift + F10, to make and run the project. The resources and images folders, will be created automatically inside the out folder.

如果您是手动操作:

快速参考代码示例(尽管需要考虑更多细节,请提供一些额外的说明链接):

package swingtest;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
 * Created with IntelliJ IDEA.
 * User: Gagandeep Bali
 * Date: 7/1/14
 * Time: 9:44 AM
 * To change this template use File | Settings | File Templates.
 */
public class ImageExample {

    private MyPanel contentPane;

    private void displayGUI() {
        JFrame frame = new JFrame("Image Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        contentPane = new MyPanel();

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private class MyPanel extends JPanel {

        private BufferedImage image;

        public MyPanel() {
            try {
                image = ImageIO.read(MyPanel.class.getResource("/resources/images/planetbackground.jpg"));
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return image == null ? new Dimension(400, 300): new Dimension(image.getWidth(), image.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, this);
        }
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new ImageExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

这篇关于在运行作为 JAR 存档分发的项目时加载图像等资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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