Java Swing: Displaying images from within a Jar

后端 未结 5 1274
野趣味
野趣味 2020-11-22 15:13

When running a Java app from eclipse my ImageIcon shows up just fine.

But after creating a jar the path to the image obviously gets screwed up.

Is there a wa

5条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 16:07

    In netbeans 8.1 what I've done is to include the folder of icons and other images called Resources inside the src folder in the project file. So whenever i build Jar file the folder is included there.The file tree should be like this:

    • src (Java files in source packges are here)
    • ** PACKAGE YOU NAMED IN PROJECT**

      • file.java
    • Resources

      • image.jpg

    The code should be like:

    jToggleButton1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/resources/image.jpg")));
    

提交回复
热议问题