加载图标图像异常 [英] Load Icon Image Exception
问题描述
我有我的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的例子。
快速参考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屋!