【发布时间】:2015-11-25 17:57:39
【问题描述】:
我在 Netbeans 中创建了一个 GUI,以从用户那里获取某些字段的输入。我不明白如何将所选 JTextField 和单选按钮中的文本保存到文本文件中。
我附上了用户表单的图片。
一旦按下“确定”按钮,用户就会得到一个对话框来保存文件。
目前,我可以将文件保存为文本文件。但是文本文件中什么也没有出现。如何从每个文本字段和单选按钮中检索数据?
请帮忙,我尝试了很多解决方案,但都无法正常工作。
private void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
String filename = JOptionPane.showInputDialog(null, "Enter the name to be saved", "File Name", 1 );
FileOutputStream writer = new FileOutputStream(filename+".dat");
txtFirstName.getText().toString(); //Trying to get text from First Name field.
writer.close();
JOptionPane.showMessageDialog(null,"Saved Successfully");
}catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
【问题讨论】:
标签: java user-interface netbeans jdialog fileoutputstream