Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have 6 JButtons on my GUI all have images on it, when I compile and run the code, all images on JButtons show up perfectly but in runnable JAR file, images on JButtons are not showing up.. how do I fix this problem?

I used this method in my code to show icons on JButtons

ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png");
jb1 = new JButton(SettingsIc);
jb1.setFocusPainted( false );
//jb1.setBorderPainted(false); 
jb1.setContentAreaFilled(false);

This is how my GUI looks when I compile my code in Eclipse enter image description here

This is how my GUI looks after executing Runnable JAR file enter image description here

See Question&Answers more detail:os

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

1 Answer

This (as pointed out by a number of people)

ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png");

Suggests that you are trying to load the images from the bin/images off the file systems. This is a relative path from the execution point of your application.

ImageIcon won't complain if the file does not exist.

If possible, you are better off embedding the resources within your Jar file (it will make it easier to deploy) and use something like getClass().getResource("/bin/images/settings.png") to load the images.

If possible, you should try using ImageIO.read(URL) to load your images, it will throw an exception if the resource pointed to by the File/URL does not exist (or is invalid).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...