How to add an ImageIcon to a JFrame?

后端 未结 3 418
梦毁少年i
梦毁少年i 2020-12-11 05:12

I\'m trying to add an image to one frame but it seems it does not working. The image created by an ImageIcon from the specified file. The image file is in the seam directory

相关标签:
3条回答
  • 2020-12-11 05:32

    If your icon is beside the TimeFrame java file, you should use

    java.net.URL imgUrl = getClass().getResource("me.jpg");
    ImageIcon icon = new ImageIcon(imgUrl);
    

    or

    java.net.URL imgUrl = TimeFrame.class.getResource("me.jpg");
    ImageIcon icon = new ImageIcon(imgUrl);
    

    You are (probably) currently looking for it in your working directory which you can output via

    System.out.println(System.getProperty("user.dir"));
    
    0 讨论(0)
  • 2020-12-11 05:41

    Will u try this one?

     ImageIcon ImageIcon = new ImageIcon("me.jpg");
        Image Image = ImageIcon.getImage();
        this.setIconImage(Image);
    
    0 讨论(0)
  • 2020-12-11 05:43

    Simply change the directory to "src/me.jpg"

    0 讨论(0)
提交回复
热议问题