开发者

ImageIcon not displayed after creating jar

开发者 https://www.devze.com 2023-02-23 13:40 出处:网络
i\'m trying to make a small project in java with no luck if i\'m compiling the program with eclipse everything is good, but when i\'m i creating the jar file i get a blank window

i'm trying to make a small project in java with no luck if i'm compiling the program with eclipse everything is good, but when i'm i creating the jar file i get a blank window

this is the image i declared for:

public ImageIcon BACKGROUND = new ImageIcon() ;

I have tried to do the following stuff:

1.

new ImageIcon("Images/wood.jpg").getImage());

2.

this.BACKGROUND.setImage(Toolkit.getDefaultToolkit().getImage("Images/wood.jpg"));

3.

this.BACKGROUND = new ImageIcon(getClass().getRe开发者_如何学运维source("Images/wood.jpg"));

4.

/** Returns an ImageIcon, or null if the path was invalid. */
protected ImageIcon createImageIcon(String path,
                                           String description) {

    java.net.URL imgURL = getClass().getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

1 & 2 showing the images after compiling and 3 & 4 returns null

another think is that i'm using mac and when i'm working with windows no image is displayed after compiling.


Here's a small working example if that helps:

public class ImageIconApplet extends JApplet {
    public void init() {
        URL url = getClass().getResource("/images/WhiteFang34.jpg");
        ImageIcon icon = new ImageIcon(url);
        JLabel label = new JLabel(icon, JLabel.CENTER);
        add(label);
    }
}

The jar for the applet on that page contains two files:

/com/whitefang34/ImageIconApplet.class
/images/WhiteFang34.jpg

I'm not sure if you're deploying an applet or a desktop swing app, however the code to load images and the packaging requirements are the same either way.


I have something...

ImageIcon icon = new ImageIcon(getClass().getResource("/package/image.png"));
JFrame.setIconImage(icon.getImage());

Place this inside your constructor.


Are you sure Images/wood.jpg is present in the .jar file?

I suggest you unzip the jar file and make sure that it's there. Otherwise you'll have to revise your build scripts (or what ever technique you use) that builds the jar.

Related questions:

  • adding non-code resources to jar file using Ant
  • Add image to JAR Java
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号