0

I wrote the following code:

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

It is showing the error:

ImageIcon cannot be resolved to a type

Can someone please help me in correcting this error?

Thank you!!!

mortalis
  • 2,060
  • 24
  • 34
SHIVAM GOYAL
  • 71
  • 1
  • 2
  • 9

2 Answers2

2

Add an import for javax.swing.ImageIcon at the top of your class or add javax.swing. before every call to ImageIcon like so:

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

Also as suggested by @MadProgrammer just keep it as an ImageIcon, there is no need to create an Image, just do it like this:

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);
sorifiend
  • 5,927
  • 1
  • 28
  • 45
1

Replace Image with ImageIcon

ImageIcon img= new ImageIcon(this.getClass().getResource("/timeplanner11.jpg")).getImage();
John Joe
  • 12,412
  • 16
  • 70
  • 135