即使在“取消”时也使用 JFileChooser showSaveDialog 保存文件

发布于 01-01 08:54 字数 1398 浏览 5 评论 0原文

我保存了一个文件,除了一个问题之外,工作正常。当按下取消按钮时,文件的副本将保存在 java 目录中。仅当按下取消按钮时才会发生这种情况,如果使用保存按钮,文件最终会到达用户选择的位置。如何阻止这种情况发生,以便按下取消按钮时不会在任何地方保存任何内容?

我的代码如下,感谢所有帮助。谢谢

    // Save dialog
private void savePlaylist() {
JFileChooser savePlaylistDialog = new JFileChooser();
                savePlaylistDialog.setSelectedFile(new File(newPlaylistNameTxt.getText() + ".txt"));
                savePlaylistDialog.showSaveDialog(playlistDialogs);
                File savePlaylist = savePlaylistDialog.getSelectedFile();

                try {
                    outFile = new PrintWriter(new FileWriter(savePlaylist));
                    outFile.println(newPlaylistInformationTxt.getText());
                    outFile.close();

                    // Plays a sound when play() is called (edited from Bombard)
                    try {
                        Clip saveButtonSound = AudioSystem.getClip();
                        AudioInputStream ais = AudioSystem.getAudioInputStream(new File("Tri-tone.wav"));
                        saveButtonSound.open(ais);
                        saveButtonSound.start();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "File could not be written, try again.");
                }
}

I have a file saved that works fine apart from one problem. When the cancel button is pressed a copy of the file is saved in the java directory. This only happens when the cancel button is pressed, if the save button is used the file ends up where the user selects. How can I stop this happening so when the cancel button is pressed nothing is saved anywhere?

My code is below, all help appreciated. Thanks

    // Save dialog
private void savePlaylist() {
JFileChooser savePlaylistDialog = new JFileChooser();
                savePlaylistDialog.setSelectedFile(new File(newPlaylistNameTxt.getText() + ".txt"));
                savePlaylistDialog.showSaveDialog(playlistDialogs);
                File savePlaylist = savePlaylistDialog.getSelectedFile();

                try {
                    outFile = new PrintWriter(new FileWriter(savePlaylist));
                    outFile.println(newPlaylistInformationTxt.getText());
                    outFile.close();

                    // Plays a sound when play() is called (edited from Bombard)
                    try {
                        Clip saveButtonSound = AudioSystem.getClip();
                        AudioInputStream ais = AudioSystem.getAudioInputStream(new File("Tri-tone.wav"));
                        saveButtonSound.open(ais);
                        saveButtonSound.start();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "File could not be written, try again.");
                }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神仙妹妹2025-01-08 08:54:00

savePlaylistDialog.showSaveDialog(playlistDialogs);

上面的方法调用返回一个 int。您需要检查它的值 - 如果用户单击“保存”按钮,它将返回 JFileChooser.ACCEPTED_OPTION。在这种情况下,您将获取返回值(可以接受/保存或取消),忽略它,然后继续将数据写入磁盘。

savePlaylistDialog.showSaveDialog(playlistDialogs);

That method call above returns an int. You need to check its value - if the user clicked on the Save button, it would return JFileChooser.ACCEPTED_OPTION. In this case, you are taking the return value (which could be accepted/save or cancel), ignoring it, and proceeding to write the data to disk anyway.

小嗷兮2025-01-08 08:54:00

这是我使用的固定代码:

    // Save dialog
    private void savePlaylist() {
    JFileChooser savePlaylistDialog = new JFileChooser();
    savePlaylistDialog.setSelectedFile(new File(newPlaylistNameTxt.getText() + ".txt"));
    int status = savePlaylistDialog.showSaveDialog(playlistDialogs);

    try {
        if (status == JFileChooser.APPROVE_OPTION) {
            //User has pressed save button

            File savePlaylist = savePlaylistDialog.getSelectedFile();

            outFile = new PrintWriter(new FileWriter(savePlaylist));
            outFile.println(newPlaylistInformationTxt.getText());
            outFile.close();

            // Plays a sound when play() is called (edited from Bombard)
            try {
                Clip saveButtonSound = AudioSystem.getClip();
                AudioInputStream ais = AudioSystem.getAudioInputStream(new File("Tri-tone.wav"));
                saveButtonSound.open(ais);
                saveButtonSound.start();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (status == JFileChooser.CANCEL_OPTION) {
            // User has pressed cancel button
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "File could not be written, try again.");
    }
}

Here is the fixed code I used:

    // Save dialog
    private void savePlaylist() {
    JFileChooser savePlaylistDialog = new JFileChooser();
    savePlaylistDialog.setSelectedFile(new File(newPlaylistNameTxt.getText() + ".txt"));
    int status = savePlaylistDialog.showSaveDialog(playlistDialogs);

    try {
        if (status == JFileChooser.APPROVE_OPTION) {
            //User has pressed save button

            File savePlaylist = savePlaylistDialog.getSelectedFile();

            outFile = new PrintWriter(new FileWriter(savePlaylist));
            outFile.println(newPlaylistInformationTxt.getText());
            outFile.close();

            // Plays a sound when play() is called (edited from Bombard)
            try {
                Clip saveButtonSound = AudioSystem.getClip();
                AudioInputStream ais = AudioSystem.getAudioInputStream(new File("Tri-tone.wav"));
                saveButtonSound.open(ais);
                saveButtonSound.start();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (status == JFileChooser.CANCEL_OPTION) {
            // User has pressed cancel button
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "File could not be written, try again.");
    }
}
穿越时光隧道2025-01-08 08:54:00

无论用户是否取消,showSaveDialog 都应该返回,并且您的代码应该采取相应的操作。此时,无论用户在保存对话框中执行什么操作,您都会保存。

showSaveDialog should return whether the user canceled or not and you code shoul act accordingly. At the moment you save no matter what the user did in the save dialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文