从 JOptionPane 读取用户输入并将其存储在文本文件中
Reading user's input from a JOptionPane and store it in a text file
我想从 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");
}
}
}
JOptionPane.showInputDialog
return 用户在字段中输入的值或 null
如果取消对话框。分配并检查 return 结果
String nickName = JOptionPane.showInputDialog(null, "Enter your nickname:");
if (nickName != null) {
// Save it...
}
有关详细信息,请参阅 How to Make Dialogs and JOptionPane#showInputDialog
我想从 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");
}
}
}
JOptionPane.showInputDialog
return 用户在字段中输入的值或 null
如果取消对话框。分配并检查 return 结果
String nickName = JOptionPane.showInputDialog(null, "Enter your nickname:");
if (nickName != null) {
// Save it...
}
有关详细信息,请参阅 How to Make Dialogs and JOptionPane#showInputDialog