简体   繁体   中英

JWindow won't display ImageIcon

I am wondering why my JWindow won't display my image. I have this code:

JWindow window = new JWindow();
JPanel jp = new JPanel();
JLabel l = new JLabel(new ImageIcon("GenericApp.png"));
jp.add(l);
window.add(jp);
window.setBounds(500, 150, 300, 200);
window.setVisible(true);
try {
    Thread.sleep(5000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
window.setVisible(false);

EDIT: My code is now (Back to original):

final JWindow window = new JWindow();
JPanel jp = new JPanel();
JLabel l = new JLabel(new ImageIcon(Asset.class.getResource("GenericApp.png")));
jp.add(l);
window.add(jp);
window.setBounds(500, 150, 300, 200);
window.setVisible(true);
try{
    Thread.sleep(5000);
}catch(InterruptedException e){
    e.printStackTrace();
}
window.setVisible(false);

Yes, I decided to use Thread.sleep() . My jar executes from /Users/thomasokeeffe/Desktop/eclipse/AI/AI, and that is where the picture is. I still get a blank JWindow when I launch. And System.out.println(new File("GenericApp.png").exists()); returns true.

Yet another edit will it be easier for me if I put the picture in the jar file? One of the weirdest things is that when I run the jar not in the app and put the picture in the working directory, it still shows a blank rectangle.......

EDIT By now I have tried using a URL, and debugging by getting the user.dir system property. I have these two line of code for getting the picture now (the picture is in the same package as the class):

URL url = this.getClass().getResource("GenericApp.png");
JLabel l = new JLabel(new ImageIcon(url));

I have also tried using an InputStream:

InputStream is = this.getClass().getResourceAsStream("GenericApp.png");
Image image = null;
try {
    image = ImageIO.read(is);
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

What am I doing wrong?

If the image is in your jar or generally in your classpath, you can get a hold of it by

getClass().getResource(resourcePath);

Where resource path is rooted at the roots of your classpath.

For instance, an image in the same dir as your class would be

new JLabel(new ImageIcon(getClass().getResource("GenericApp.png")));

Gui code works with event calls. While your function is executing in the main thread nothing will change on screen. Actual changes on screen are done between calls to your code. This means that your functions should be finished as quickly as possible.

So to get something to show you need to return.

To queue some code to execute later you can use a javax.swing.Timer:

JWindow window = new JWindow();
JPanel jp = new JPanel();
JLabel l = new JLabel(new ImageIcon("GenericApp.png"));
jp.add(l);
window.add(jp);
window.setBounds(500, 150, 300, 200);
window.setVisible(true);
Timer timer = new Timer(5000, new ActionListener(){
    void actionPerformed(ActionEvent e){
        window.setVisible(false);
    }
});
timer.setRepeats(false);//only do this once
timer.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM