Java: how to add image to Jlabel?

后端 未结 5 649
悲哀的现实
悲哀的现实 2020-11-28 12:45
Image image = GenerateImage.toImage(true); //this generates an image file
JLabel thumb = new JLabel();
thumb.setIcon(image)
5条回答
  •  星月不相逢
    2020-11-28 13:12

    You have to supply to the JLabel an Icon implementation (i.e ImageIcon). You can do it trough the setIcon method, as in your question, or through the JLabel constructor:

    Image image=GenerateImage.toImage(true);  //this generates an image file
    ImageIcon icon = new ImageIcon(image); 
    JLabel thumb = new JLabel();
    thumb.setIcon(icon);
    

    I recommend you to read the Javadoc for JLabel, Icon, and ImageIcon. Also, you can check the How to Use Labels Tutorial, for more information.

提交回复
热议问题