且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java:如何将图像添加到Jlabel?

更新时间:2023-12-03 14:20:40

你必须向JLabel提供一个 Icon 实现(即 ImageIcon )。您可以通过 setIcon 方法(如您的问题)或通过 JLabel 构造函数执行此操作:

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

我建议你阅读 JLabel 图标 ,以及 ImageIcon 。另外,您可以查看如何使用标签教程,更多信息。

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.