繁体   English   中英

如何使用JFilechooser和Jlabel查看图片

[英]How to view pictures using JFilechooser and Jlabel

大家好,我想问一下如何使用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);

    }
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM