【发布时间】:2013-12-16 09:47:01
【问题描述】:
我创建了一个文本编辑器和一个保存按钮,我需要创建一个绝对查找器,这样如果用户没有输入 .txt,程序会自动执行它,因此它始终保存为 txt 文件。请帮忙?
保存按钮的代码
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooseFile = new JFileChooser();
int choosing = chooseFile.showSaveDialog(this);
if ( choosing == JFileChooser.APPROVE_OPTION)
{
try {
PrintWriter fileSave = new PrintWriter(chooseFile.getSelectedFile());
//absolute path ends with
fileSave.printf(txtArea.getText());
fileSave.close();
txtStatus.setText("Saved");
} catch (FileNotFoundException ex) {
Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
【问题讨论】:
-
你不能检查
chooseFile.getSelectedFile()并在需要时附加“.txt”吗?