加载图标资源错误 [英] Loading Icon resource error
问题描述
在Eclipse中,当我运行代码时,这可行:
In Eclipse, when I run the code, this works:
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("test viewing images");
frame.setSize(600,300);
frame.setLocationRelativeTo(null); // centered on monitor
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/**
* Menu Bar stuff
*/
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
// MENU BAR
menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
menuBar.setVisible(true);
// MENU 1
menu = new JMenu("File");
menuBar.add(menu);
// MENU 1 ITEM
ImageIcon icon = new ImageIcon("src/Action-exit-icon.png");
menuItem = new JMenuItem("Exit Program", icon);
menu.add(menuItem);
frame.setVisible(true);
}
}
这是我的包资源管理器中的文件结构:
And here's the File Structure from my Package Explorer:
ShowImage (project)
> src / Main.java
> src / Action-exit-icon.png
此外,此工作空间位于Z:\ eclipse_projects
Also, this workspace is located in Z:\eclipse_projects
我可以看到 ImageIcon icon = new ImageIcon(src / Action-exit-icon.png); 工作正常, menuBar是它的工作。
I can see the ImageIcon icon = new ImageIcon("src/Action-exit-icon.png"); is working nicely, and the menuBar does it's job.
现在让我们导出这个项目,我会把JAR发给我的一个朋友。
Now let's Export this project, and I'll email the JAR to a friend of mine.
- 右键单击项目>选择导出
- 选择Java> Runnable JAR文件
- 我选择启动配置中的主文件
- 导出目标:我的桌面
- 库处理:将所需库提取到生成的JAR中
- 转到我的桌面,双击ShowImage.jar
- Right-click project > Select Export
- Select Java > Runnable JAR File
- I choose the Main File in Launch configuration
- Export destination: my desktop
- Library handling: Extract required libraries into generated JAR
- go to my desktop, double-click the ShowImage.jar
JFrame显示,但 Action-exit -icon.png 根本没有出现。
The JFrame shows up, but the Action-exit-icon.png isn't appearing at all.
当我打开ShowImage.jar,查看它的内容时,我看到Main.class, Action-exit-icon.png,META-INF。
When I open the ShowImage.jar, to view it's contents, I see the Main.class, Action-exit-icon.png, META-INF.
好的,我对如何引用图像或任何资源感到非常困惑。
我做错了什么?
Ok, I'm seriously confused about how to reference an image, or any resource now. What am I doing wrong?
推荐答案
new ImageIcon("src/Action-exit-icon.png");
<$ c的字符串
构造函数$ c> ImageIcon 假定字符串代表文件
路径。
The String
constructor for an ImageIcon
presumes the string to represent a File
path.
这个图像显然是一个应用程序资源,在部署时(在Jar中)将成为一个嵌入式资源。因此,必须从应用程序的运行时类路径中通过 URL
访问它,如下所示:
This image is obviously an application resource, and will become an embedded resource by the time of deployment (in a Jar). Therefore it must be accessed by URL
from the run-time class-path of the app., like so:
new ImageIcon(getClass().getResource("/src/Action-exit-icon.png"));
检修代码,我明白了:
Overhauling the code, I get this:
import java.awt.Color;
import javax.swing.*;
public class JavaGui148 {
public JComponent getGUI() {
JPanel p = new JPanel();
p.setBackground(Color.GREEN);
return p;
}
public JMenuBar getMenuBar() {
/**
* Menu Bar stuff
*/
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
// MENU BAR
menuBar = new JMenuBar();
menuBar.setVisible(true);
// MENU 1
menu = new JMenu("File");
menuBar.add(menu);
// MENU 1 ITEM
ImageIcon icon = new ImageIcon(getClass().getResource(
"/src/Action-exit-icon.png"));
menuItem = new JMenuItem("Exit Program", icon);
menu.add(menuItem);
return menuBar;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JavaGui148 gui = new JavaGui148();
JFrame f = new JFrame("Demo");
f.setJMenuBar(gui.getMenuBar());
f.add(gui.getGUI());
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
这篇关于加载图标资源错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!