簡體   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