Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

java - Problems with images when compiled

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();
}

}

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With your current directory setup, the images dir won't even get built into the jar. Try to extract it and you will most likely see that it's not in there.

You can tell by the fact that it doesn't have the little package logo in the folder, as seen here with resources

enter image description here

The only default directory built into the classpath (/jar) is the src. We need to either put the resources into the src

enter image description here

or configure the build path to include the files that are in the resources. Once we do that, we will see the little package icon inside the folder icon. That's how we know the files are on the build path

enter image description here


CODE we would use:

  • First Image: Can't, it won't work (this your current predicament)

  • Second Image:

    ImageIcon icon = new ImageIcon(
             getClass().getResource("/resources/stackoverflow.png"));
    
  • Third Image:

    ImageIcon icon = new ImageIcon(
             getClass().getResource("/stackoverflow.png"));
    

To configure the build path to use the third option, follow the instructions in Example 2 in this answer


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...