ImageIcon loads no image
I'm trying to get image built from tiled set of images So to JPanel I'm adding JButtons with ImageIcons. All images are in folder with my classes (NetBeans), and they're named u1, u2, ..., u16. But on button there is no image shown. What am I doing wrong ?
JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));
for (int i = 1; i < 17; i++) {
JLabel l = new JLabel(new Imag开发者_开发技巧eIcon("u"+i+".jpg"), JLabel.CENTER);
l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
panel.add(l);
}
NetBeans is probably not finding your image files. To test this, use the full name of the files (from the C:/path/to/image.jpg
if you are in Windows, and from the /path/to/image.jpg
if you are under something unix-like).
I recommend using
new ImageIcon(this.getClass().getResource(fileName));
where fileName
for something available at "classes/org/myorg/resources/image.jpg" should look like org/myorg/resources/image.jpg
. This has the advantage that it will work regardless of the way you access your classes (through HTTP, in a jar-file, ...).
精彩评论