在运行作为JAR存档分发的项目时加载图像之类的资源 [英] Loading resources like images while running project distributed as JAR archive
问题描述
我的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 文件夹.选择新建->包
- 在新软件包对话框下,键入软件包的名称,例如 资源 .点击确定
- 右键点击资源包.选择新建->包
- 在新软件包对话框下,键入软件包的名称,例如 图像 .点击确定
- 现在选择要添加到项目中的图像,然后将其复制.在 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.
如果您手动进行操作:
- 如何将图像添加到项目中
- 如何使用图标
- 一些额外的澄清 ,如该答案的第一个 代码示例.
- 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);
}
}
这篇关于在运行作为JAR存档分发的项目时加载图像之类的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!