我正在尝试向我的 JLabel
添加图标,但该图标没有出现。该图标是一个图像,与我的 Frame 类文件位于同一位置。为什么图标没有出现?
这是我的代码:
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"));
关于java - 为什么我的 JLabel 图标没有出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32169036/