Rendering Image into label

a 夏天 提交于 2019-12-11 14:25:22

问题


I have a label. I want to render image into it. But the following code would do anything.

 CardLayout cl = (CardLayout) (mainPanel.getLayout());
        cl.show(mainPanel, "newPersonaCard");
        BufferedImage myPicture = ImageIO.read(new    File("C:\\Desktop\\Documents\\Pictures\\always.jpg"));
        ImageIcon icon = new ImageIcon(myPicture);
        icon.getImage().flush();

I am using netbean designer.


回答1:


You are right, in some cases there issue with repainting Icon in the JLabel, then you have to call,

myIcon.getImage().flush();
myLabel.setIcon(myIcon);

rest of methods is implemented in the Icon and JLabel correctly




回答2:


.
.
File file = fileChooser.getSelectedFile();

JLabel label = new JLabel();
ImageIcon icon = new ImageIcon(file.getAbsolutePath());
label.setIcon(icon);
//add label to panel



回答3:


fileChooser.showDialog(saveBtn2, null);
File file = fileChooser.getSelectedFile();
System.out.println("The path to file "+file.getAbsolutePath());          
ImageIcon icon = new ImageIcon(file.getAbsolutePath());
pictureLbl.setIcon(icon);


来源:https://stackoverflow.com/questions/10318502/rendering-image-into-label

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!