0

我写了以下代码:

JLabel lblNewLabel_1 = new JLabel("");
Image img= new ImageIcon(this.getClass().getResource("/timeplanner11.jpg")).getImage();
lblNewLabel_1.setIcon(new ImageIcon(img));
lblNewLabel_1.setBounds(0, 0, 28, 30);
panel.add(lblNewLabel_1);

它显示错误:

ImageIcon cannot be resolved to a type

有人可以帮我纠正这个错误吗?

谢谢!!!

4

2 回答 2

1

替换ImageImageIcon

ImageIcon img= new ImageIcon(this.getClass().getResource("/timeplanner11.jpg")).getImage();
于 2017-04-21T06:36:28.220 回答
1

在类的顶部添加 javax.swing.ImageIcon 的导入,或者javax.swing.在每次调用之前添加,ImageIcon如下所示:

javax.swing.ImageIcon icon = new javax.swing.ImageIcon(this.getClass().getResource("/timeplanner11.jpg"));
lblNewLabel_1.setIcon(icon);

同样正如@MadProgrammer 所建议的,只需将其保留为 ImageIcon,无需创建图像,只需这样做:

JLabel lblNewLabel_1 = new JLabel("");
ImageIcon icon = new ImageIcon(this.getClass().getResource("/timeplanner11.jpg"));
lblNewLabel_1.setIcon(icon);
lblNewLabel_1.setBounds(0, 0, 28, 30);
panel.add(lblNewLabel_1);
于 2017-04-21T04:31:49.400 回答