Image image = GenerateImage.toImage(true); //this generates an image file
JLabel thumb = new JLabel();
thumb.setIcon(image)
问问题
211987 次
5 回答
35
您必须向 JLabel 提供一个Icon
实现(即ImageIcon
)。您可以通过setIcon
方法(如您的问题)或通过JLabel
构造函数执行此操作:
Image image=GenerateImage.toImage(true); //this generates an image file
ImageIcon icon = new ImageIcon(image);
JLabel thumb = new JLabel();
thumb.setIcon(icon);
于 2010-10-14T08:00:32.030 回答
25
要从 URL 获取图像,我们可以使用以下代码:
ImageIcon imgThisImg = new ImageIcon(PicURL));
jLabel2.setIcon(imgThisImg);
它完全适合我。PicUrl 是一个字符串变量,用于存储图片的 url。
于 2012-10-15T05:33:38.600 回答
12
(如果您使用的是 NetBeans IDE)只需在项目中创建一个文件夹,但在 src 文件夹之外。将文件夹命名为 Images。然后将图像放入Images文件夹并在下面编写代码。
// Import ImageIcon
ImageIcon iconLogo = new ImageIcon("Images/YourCompanyLogo.png");
// In init() method write this code
jLabelYourCompanyLogo.setIcon(iconLogo);
现在运行你的程序。
于 2013-11-08T11:26:58.250 回答
4
最短的代码是:
JLabel jLabelObject = new JLabel();
jLabelObject.setIcon(new ImageIcon(stringPictureURL));
stringPictureURL是图像的路径。
于 2015-03-13T20:59:39.633 回答
1
您可以在main(String[] args)函数中编写的简单代码
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//application will be closed when you close frame
frame.setSize(800,600);
frame.setLocation(200,200);
JFileChooser fc = new JFileChooser();
if(fc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION){
BufferedImage img = ImageIO.read(fc.getSelectedFile());//it must be an image file, otherwise you'll get an exception
JLabel label = new JLabel();
label.setIcon(new ImageIcon(img));
frame.getContentPane().add(label);
}
frame.setVisible(true);//showing up the frame
于 2014-10-16T20:51:32.917 回答