加载图标图像异常 [英] Load Icon Image Exception

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

问题描述

我有我的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);

这是我收到的错误:

Here is the error I am getting:

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)

图片是在正确的目录其中的资源文件夹的根
项目文件

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"));

更多信息后,就在何处在于这两种方法之间的差异,可以在这个线程发现 - 的 <装载STRONG>不同的方式资源

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 选择粘贴

  • 使用的最后一个环节,检查现在怎么访问此文件中的Java code。虽然在这个例子中,人们将使用

  • 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(/资源/图像/ myImage.imageExtension);

preSS <大骨节病>移 + <大骨节病> F10 ,制造和运行项目。在资源和图像文件夹,将自动在退出文件夹内创建。

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

如果你正在做手工:

快速参考code实例(尽管更详细的考虑,一点点额外的澄清链接):

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);
    }
}

这篇关于加载图标图像异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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