问题
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
Also, this workspace is located in Z:\eclipse_projects
I can see the ImageIcon icon = new ImageIcon("src/Action-exit-icon.png"); is working nicely, and the menuBar does it's job.
Now let's Export this project, and I'll email the JAR to a friend of mine.
- 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
The JFrame shows up, but the Action-exit-icon.png isn't appearing at all.
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?
回答1:
new ImageIcon("src/Action-exit-icon.png");
The String
constructor for an ImageIcon
presumes the string to represent a File
path.
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);
}
}
来源:https://stackoverflow.com/questions/13943913/loading-icon-resource-error