[英]How to view pictures using JFilechooser and Jlabel
Hi guys I want to ask how can I view Images using Jlabel and attach that image using Jfilechooser? 大家好,我想问一下如何使用Jlabel查看图像并使用Jfilechooser附加图像?
what code do I use so that when I attached a image it will automatically shown in the Jlabel? 我使用什么代码,以便在附加图像时将其自动显示在Jlabel中?
Thanks in advance 提前致谢
Here is my JFileChooser code: 这是我的JFileChooser代码:
JFileChooser image=new JFileChooser();
image.showOpenDialog(null);
File f=image.getSelectedFile();
filename=f.getAbsolutePath();
txtFilename.setText(filename);
try{
File images=new File(filename);
FileInputStream fis=new FileInputStream(images);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte[] buf=new byte[1024];
for(int readNum;(readNum=fis.read(buf))!=-1;){
bos.write(buf,0,readNum);
}
person_image=bos.toByteArray();
}
catch(IOException e){
JOptionPane.showMessageDialog(null,e);
}
Image image = null;
try {
JFileChooser fc=new JFileChooser();
fc.showOpenDialog(null);
File f=fc.getSelectedFile();
image = ImageIO.read(f);
} catch (IOException e) {
}
// Use a label to display the image
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(image));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.