如何使用 JFileChooser 保存图像

How to save an image using JFileChooser

背景信息:我制作了一个使用 JFileChooser 上传图像的程序,并填充了 JFrame 的 space。

我的问题: 我已经尝试在保存按钮上实现我的方法,到目前为止我可以调出 JFileChooser,但它实际上不会保存图像。那么我将如何保存我之前使用 JFileChooser 上传的相同图像呢?我看过很多例子,但不是很理解。

我的浏览img代码:

// When button pressed, allows user to browse inventory
BrowseButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JFileChooser file = new JFileChooser();
        file.setCurrentDirectory(new File(System.getProperty("user.home")));
        // Filter files
        FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg",
                "png");
        file.addChoosableFileFilter(filter);
        int res = file.showSaveDialog(null);
        if(res == JFileChooser.APPROVE_OPTION) {
            File selFile = file.getSelectedFile();
            String path = selFile.getAbsolutePath();
            label.setIcon(resize(path));
        } // End if
    } // End actionPerformer
}); // End ActionListener

我的保存img代码:

// Save file
saveButton.addActionListener(new ActionListener() {    
    public void actionPerformed(ActionEvent e) {
        JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileFilter(new FileNameExtensionFilter("*.png", "png"));
            if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                String img = file.getAbsolutePath();
            } // End if
        } // End actionPerformed
    }); // End ActionListener

更新保存 img 代码:

    // Save getFile
    saveButton.addActionListener(new ActionListener() {    
        public void actionPerformed(ActionEvent e) {
            JFileChooser saveFile = new JFileChooser();
            saveFile.setCurrentDirectory(new File(System.getProperty("user.home")));
            // Filter files
            FileNameExtensionFilter filter2 = new FileNameExtensionFilter(".Images", "jpg", "png");
            saveFile.addChoosableFileFilter(filter2);
            int f1 = saveFile.showSaveDialog(null);
            if(f1 == JFileChooser.APPROVE_OPTION) {
                File file = saveFile.getSelectedFile();
                    try {
                        ImageIO.write(selFile, "png", file);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                } // End if
            } // End actionPerformed
        }); // End ActionListener
            
BufferedImage getImg;
private String path1;  
private final JButton saveButton = new JButton("Save");  

saveButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser getFile = new JFileChooser();
            getFile.setCurrentDirectory(new File(System.getProperty("user.home")));
            // Filter files
            FileNameExtensionFilter filter1 = new FileNameExtensionFilter("*.Images", "jpg",
                    "png");
            getFile.addChoosableFileFilter(filter1);
            int res = getFile.showSaveDialog(null);
            if(res == JFileChooser.APPROVE_OPTION) {
                selFile1 = getFile.getSelectedFile();
                path1 = selFile1.getAbsolutePath();
                label.setIcon(resize(path1));
                System.out.println("1st selFile1 = " + selFile1);                    
                try {
                    ImageIO.write(getImg, "jpg", selFile1);
                } catch (IOException ex) {
                }
            } // End if
        } // End actionPerformer
    }); // End ActionListener