简体   繁体   中英

ImageIcon won't display in JLabel

I'v been trying to use Image Icon for my Pokemon game by showing an image of each start when you are picking them. However, the image doesn't show up when I run my program yet it doesn't throw an errors. The image is in a folder named images in the src folder. I'v been learning Swing by myself pretty well, but this problem stumped me.

public ImageIcon f = new ImageIcon("images/bulbasaur.png"); //declared in the bulbasaur class

Bulbasaur bulb = new Bulbasaur(1, "Bulbasaur", 5);    // declared in the main class
final JLabel test = new JLabel();
test.setIcon(bulb.f);
test.setVisible(true);

Java may be unable to find the image with the path given I would try changing your first line

public ImageIcon f = new ImageIcon(Bulbasaur.class.getResource("images/bulbasaur.png"));

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