I have been all over the internet trying to work out how to get an image icon displayed after compiling into a runnable jar. I discovered this problem way too late, I ran my program many times before in eclipse and every thing has worked, now 6 months later with project finished, I compiled my program with eclipse and no audio or images work. Reading on the net, it says about the location of images folder should be inside jar, but mine doesnt get put there?
I have played around with the images folder moving it inside the source folder, but it didn't work. I have a feeling that it might be something to do with the path of the resource...ebut thats only guessing.
I have built a simple program that has the same results... works when ran in eclipse, but not when compiled. Could somebody show me an example by modifying my code below. Thanks in advance.
SOURCE CODE:
package ImageIcon;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Gui {
public static JLabel c;
public Gui(){
JFrame f = new JFrame();
JPanel p = new JPanel();
p.setBounds(0, 0, 120, 200);
p.setBackground(Color.black);
p.setLayout(null);
JPanel bg = new JPanel(new BorderLayout());
bg.setBounds(50, 50, 15, 15);
bg.setBackground(Color.white);
ImageIcon a = new ImageIcon("images/success.jpg");
c = new JLabel(a);
f.setSize(100, 200);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.add(p);
p.add(bg);
bg.add(c);
}
public static void main(String[] args) {
new Gui();
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…