为什么我的 JLabel 的图标没有出现?
Why doesn't my JLabel's icon appear?
我正在尝试向我的 JLabel
添加一个图标,但该图标没有出现。该图标是一个图像,与我的 Frame
class 文件位于同一位置。为什么没有出现图标?
这是我的代码:
public Passing()
{
initComponents();
ImageIcon imageIcon = new ImageIcon("altis.jpg");
jLabel1.setIcon(imageIcon);
//I have also tried: jLabel1 = new JLabel(new ImageIcon("/res/altis.jpg"));
}
尝试在 src
目录中创建一个名为 res
的文件夹,将图标放在 res
中,然后替换此行:
ImageIcon imageIcon = new ImageIcon("altis.jpg");
有了这个:
ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("/res/altis.jpg"));
(或者如果这不起作用):
ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("res/altis.jpg"));
我正在尝试向我的 JLabel
添加一个图标,但该图标没有出现。该图标是一个图像,与我的 Frame
class 文件位于同一位置。为什么没有出现图标?
这是我的代码:
public Passing()
{
initComponents();
ImageIcon imageIcon = new ImageIcon("altis.jpg");
jLabel1.setIcon(imageIcon);
//I have also tried: jLabel1 = new JLabel(new ImageIcon("/res/altis.jpg"));
}
尝试在 src
目录中创建一个名为 res
的文件夹,将图标放在 res
中,然后替换此行:
ImageIcon imageIcon = new ImageIcon("altis.jpg");
有了这个:
ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("/res/altis.jpg"));
(或者如果这不起作用):
ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("res/altis.jpg"));