JFileChooser

Swing:点击图片后上传一张图片

 if(buttonName.equals("upload a picture")) {
             JFileChooser choose = new JFileChooser();
             choose.setMultiSelectionEnabled(false);
             FileNameExtensionFilter filter = new FileNameExtensionFilter("jpg","png");
             choose.setFileFilter(filter);
             int returnVar = choose.showOpenDialog(null);
             if(returnVar == choose.APPROVE_OPTION){
                 File f = choose.getSelectedFile();
                 String fileName =f.getName();
                 String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
                 if(!(prefix.equals("jpg") || prefix.equals("png"))){
                        JOptionPane.showMessageDialog(new JDialog(),":please choose '.jpg' or '.png' type files");
                        return;
                    }
                 absolutePath = choose.getSelectedFile().getAbsolutePath();
                 ImageIcon imageIcon = new ImageIcon(absolutePath);
                 imageIcon = new ImageIcon(absolutePath);
                 Image img = imageIcon.getImage();
                 img = img.getScaledInstance(100, 100, Image.SCALE_DEFAULT);
                 lblPicture.setIcon(imageIcon);
                 
             }

https://blog.csdn.net/qq_40064948/article/details/81272300

你可能感兴趣的:(JFileChooser)