Search code examples
javaswingfile-ioimageicon

Java Icon not working


So i have this code

import javax.swing.*;


public class pictest {

public static void main(String[] args) {
    JFrame frame = new JFrame("Label Example");
    ImageIcon mine = new ImageIcon("‪C:/Users/Eric/Desktop/mine.jpg");
    JLabel pic = new JLabel(mine);
    frame.add(pic);
    frame.setSize(300,250);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setVisible(true);

    JFrame framee = new JFrame("Label Example");
    ImageIcon minee = new ImageIcon("C:/Users/Eric/Desktop/mine.jpg");
    JLabel pice = new JLabel(minee);
    framee.add(pice);
    framee.setSize(300,250);
    framee.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    framee.setVisible(true);

 }

}

So when i run the code i get this:

https://i.sstatic.net/8wwlj.jpg

(the left pic is the code from the top part, the right is the code from the bottom part)

As far as i can tell the code is exactly the same(except for variable names) and i can not figure out why the picture will not show up on the one on the left, is this a problem where i need to reinstall stuff, or is there something in the code that i am just getting wrong. (also if someone could reformat this post to look right i would appreciate that, sorry i dont use stack overflow much but i was getting frustrated.)


Solution

  • When I try to save your code, I get the following error:

    enter image description here

    The image shows on the top frame only when I copy
    ImageIcon minee = new ImageIcon("C:/Users/Eric/Desktop/mine.jpg");

    and paste and edit it to

    ImageIcon mine = new ImageIcon("C:/Users/Eric/Desktop/mine.jpg");
    

    I guess you are using some un-recognized charactes in that line.