java – How to add an ImageIcon to a JFrame?

java – How to add an ImageIcon to a JFrame?

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));

Will u try this one?

 ImageIcon ImageIcon = new ImageIcon(me.jpg);
    Image Image = ImageIcon.getImage();
    this.setIconImage(Image);

java – How to add an ImageIcon to a JFrame?

Simply change the directory to src/me.jpg

Leave a Reply

Your email address will not be published.