简体   繁体   中英

Why doesn't my JLabel's icon appear?

I am trying to add an icon to my JLabel , but the icon does not appear. The icon is an image which is in the same location as my Frame class file. Why doesn't the icon appear?

Here is my code:

public Passing()
{
    initComponents();

    ImageIcon imageIcon = new ImageIcon("altis.jpg");
    jLabel1.setIcon(imageIcon);

    //I have also tried: jLabel1 = new JLabel(new ImageIcon("/res/altis.jpg"));
}

Try creating a folder inside your src directory called res , put the icon inside of res , and replace this line:

ImageIcon imageIcon = new ImageIcon("altis.jpg");

with this:

ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("/res/altis.jpg"));

(or if that doesn't work):

ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("res/altis.jpg"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM