大家好我想问一下如何使用Jlabel查看图像并使用Jfilechooser附加该图像?
我使用什么代码,以便在附加图像时它会自动显示在Jlabel中?
提前致谢
这是我的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);
}
答案 0 :(得分:0)
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);