How to use getClass().getResource() method

前端 未结 3 1550
自闭症患者
自闭症患者 2020-12-06 07:30

When I create ImageIcon class objects I use the following code:

iconX = new ImageIcon (getClass().getResource(\"imageX.png\"))
3条回答
  •  不知归路
    2020-12-06 07:53

    Simply supply the path to the resource.

    So, if you put the image in "/resources/images" within your Jar, you would simply use

    iconX = new ImageIcon(getClass().getResource("/resources/images/imageX.png"))
    

    Essentially what you're saying is, class loader, please search your class path for the following resource.

提交回复
热议问题