加载图片资源 [英] Loading image resource
问题描述
我的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:
- < a href =http://netbeans.org/kb/docs/java/gui-image-display.html =noreferrer> 在Java GUI应用程序中处理图像
- 如何将图像添加到项目中
- Handling Images in a Java GUI Application
- How to add Images to the Project
对于IntelliJ IDEA:
- 右键单击项目的 src 文件夹。选择新建 - >套餐
- 在新套餐对话框下,输入套餐名称,说 资源 即可。单击确定
- 右单击资源包。选择新建 - >套餐
- 在新套餐对话框下,输入套餐名称,说 images 即可。单击确定
- 现在选择要添加到项目中的图像,然后复制它。 右键单击resources.images包,在 IDE 内,选择粘贴
-
使用现在用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.
如果您是手动执行此操作:
- 如何向项目中添加图片
- 如何使用图标
- 稍加澄清 ,如本答案的第一个
代码示例所示。
- How to add Images to your Project
- How to Use Icons
- A Little extra clarification, as given in this answer's first code example.
快速参考代码示例(尽管需要更详细的考虑,有一点额外的澄清链接):
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屋!