我想从JOptionPane读取用户输入(用户的昵称),然后将该输入存储到新的txt文件中。此外,我希望每次新用户输入昵称时,输入的信息都会在文本文件中更新。非常感谢您的帮助!这是我的代码:
private static class testItApp implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String filename = "scoreboard.txt";
try {
PrintWriter output = new PrintWriter(filename);
JOptionPane.showInputDialog(null, "Enter your nickname:");
output.println("kjkjbkj");
output.close();
}
catch (FileNotFoundException ex) {
System.out.println("Error");
}
}
}
答案 0 :(得分:2)
JOptionPane.showInputDialog
返回用户在字段中输入的值,如果取消了对话框,则返回null
。分配并检查返回结果
String nickName = JOptionPane.showInputDialog(null, "Enter your nickname:");
if (nickName != null) {
// Save it...
}