Image won't display in JLabel

后端 未结 5 641
轻奢々
轻奢々 2021-01-21 03:17

I\'ve gone through every post I could find on this site and the Java tutorials and I still can\'t figure out why my code isn\'t working. Even when I copy/paste other peoples\'

5条回答
  •  清歌不尽
    2021-01-21 03:34

    You're looking for the image as a file. When you do that the searches are all done in a path relative to the user directory which you can get via

    // code not tested
    System.out.println(System.getProperty("user.dir"));
    

    So you will likely have to adjust your image's path to get it as a file. The other option is to get it as a resource as noted by Siva Charan in which case the path is relative to the location of your class files.

    Oh and once you study and use the layout managers, they become intuitive, and creating and especially maintaing your GUI's become much easier.

提交回复
热议问题