如何通过filechooser为选定的图像设置标签? [英] How to set a label with selected image via filechooser ?
本文介绍了如何通过filechooser为选定的图像设置标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我想用用户通过filechooser选择的图像设置标签.有什么办法吗?这是我的浏览按钮代码;
我要这个;当我双击图像时,它应该转到标签.:(名称为LabelPicture)
Hello everyone,
I want to set a label with the image which user select via filechooser. Is there any way to do this? Here is my Browse button code;
I want this; When I double click the image the it should go to the label.:(name is LabelPicture)
int returnValue = jFileChooser1.showOpenDialog(this);
jFileChooser1.setVisible(true);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File file = jFileChooser1.getSelectedFile();
try {
BufferedImage img = ImageIO.read(file);
} catch (IOException ex) {
Logger.getLogger(AddNewInstructor.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
//Do what you want to do when user doesn't upload anything
}
推荐答案
int returnValue = jFileChooser1.showOpenDialog(this);//Crate a filechoser
jFileChooser1.setVisible(true);//SetVisible
if (returnValue == JFileChooser.APPROVE_OPTION) {
File file = jFileChooser1.getSelectedFile();//Get the file
ImageIcon icon = new ImageIcon();//Icon Constructor
try {
BufferedImage image = ImageIO.read(file); // Read File(Image)
}
catch (IOException ex)
{
//For Exceptions
}
ImageIcon image = new ImageIcon(file.getPath());
lblresim.setIcon(image); //Set the label with the file that was taken via filechooser
} else {
JOptionPane.showMessageDialog(null,"File Choosing cancelled by user.");//If no file taken from FileChoser warn user.
}
}
这篇关于如何通过filechooser为选定的图像设置标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文