Image won't display in JLabel

后端 未结 5 643
轻奢々
轻奢々 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条回答
  •  猫巷女王i
    2021-01-21 04:00

    Try this way:-

    ImageIcon icon = createImageIcon("bg.png", "image description");
    
    protected ImageIcon createImageIcon(String path, String description) {
        java.net.URL imgURL = getClass().getResource(path);
    
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file:" +path);
            return null;
        }
    }
    

提交回复
热议问题