Java:如何将图像添加到Jlabel? [英] Java: how to add image to Jlabel?

查看:118
本文介绍了Java:如何将图像添加到Jlabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


推荐答案

你必须向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.

这篇关于Java:如何将图像添加到Jlabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆