Add images to jar

前端 未结 8 1434
孤独总比滥情好
孤独总比滥情好 2021-02-06 12:30

I want to set icon to my JFrame. I do the following:

Image icon = Toolkit.getDefaultToolkit().getImage(\"src/images/icon.jpg\");
this.setIconImage(icon);
         


        
相关标签:
8条回答
  • 2021-02-06 12:48

    For this to work, you should access the images from a directory relative to some fixed class. For example, if the image files are saved in a directory "images" on the same level as the Toolkit.class, then

    this.setIconImage(Toolkit.getDefaultToolkit().getImage(Toolkit.class.getResource("images/icon.jpg"))); should work.

    0 讨论(0)
  • 2021-02-06 12:57

    Assuming your JAR file has a top level directory called images, you can use either:

    1. getClass().getResource("/images/icon.jpg") or
    2. getClass().getClassLoader().getResource("images/icon.jpg")
    0 讨论(0)
提交回复
热议问题