简体   繁体   English

为什么我的JLabel的图标没有出现?

[英]Why doesn't my JLabel's icon appear?

I am trying to add an icon to my JLabel , but the icon does not appear. 我正在尝试将图标添加到我的JLabel ,但是该图标没有出现。 The icon is an image which is in the same location as my Frame class file. 该图标是与我的Frame类文件位于同一位置的图像。 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: 尝试在src目录中创建一个名为res的文件夹,将图标放入res ,然后替换此行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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