-1

I am setting icon to my Jlabel in Eclips but it giving error that Type mismatch: cannot convert from java.awt.Image to Image and in next line The constructor ImageIcon(Image) is undefined

    JLabel label_1 = new JLabel("");
    Image img=new ImageIcon(this.getClass().getResource("/a1.png")).getImage();
    label_1.setIcon(new ImageIcon(img));
    label_1.setBounds(0, 0, 439, 615);
    frame.getContentPane().add(label_1);
  • 1
    1) Istead of `Image img=new ImageIcon(this.getClass().getResource("/a1.png")).getImage(); label_1.setIcon(new ImageIcon(img));` why not `label_1.setIcon(new ImageIcon(this.getClass().getResource("/a1.png"));`? 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Feb 05 '17 at 11:00
  • @AndrewThompson like i do bellow – Youcef LAIDANI Feb 05 '17 at 11:00
  • [Reading/Loading Images](https://docs.oracle.com/javase/tutorial/2d/images/loadimage.html) – MadProgrammer Feb 05 '17 at 21:46

1 Answers1

2

Why you don't use Icon instead to Image :

Icon icon = new ImageIcon(getClass().getResource("/a1.png"));
label_1.setIcon(icon);
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 1
    @AdityaSingh it is customary on StackOverflow to mark a response as an answer if it answers your question. Click on the check mark to the left of the answer. If any of the responses help you (including one that answers your question), it is also customary to 'upvote' it; these give reputation points to whomever took the time to answer, and help mark which answers were helpful and on point for others looking for similar information. – arcy Feb 05 '17 at 11:09
  • `Icon img..` would better be named `Icon icon..`. – Andrew Thompson Feb 05 '17 at 11:13
  • @arcy sir it requires minimum 15 reputations to . i don't have, i am new here – Aditya Singh Feb 05 '17 at 11:52