即使在“取消”时也使用 JFileChooser showSaveDialog 保存文件
我保存了一个文件,除了一个问题之外,工作正常。当按下取消按钮时,文件的副本将保存在 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 技术交流群。

发布评论
评论(3)
小嗷兮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.");
}
}
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
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 returnJFileChooser.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.