private void OKActionPerformed(java.awt.event.ActionEvent evt)
throws IOException {
String name = this.UserName.getText().trim();//获取输入框信息
String pwd = this.PassWord.getText().trim();
Properties prop = new Properties();//创建集合对象
Reader r = new FileReader("user.txt");//读取"user.txt"文件
prop.load(r);
r.close();
Set set = prop.stringPropertyNames();
if ((name == null ||name.length()==0)||(pwd == null ||pwd.length()==0)) {
//判断输入框里的信息是否为空
JOptionPane.showMessageDialog(this, "用户名或密码不能为空,请重新输入!");
dispose();
RegistFrame re = new RegistFrame();
re.setVisible(true);
this.UserName.setText("");
this.PassWord.setText("");
} else {
if (set.contains(name)) {//判断输入框里的name是否已经被注册
JOptionPane.showMessageDialog(this, "您输入的用户名已存在,请重新输入!");
dispose();
RegistFrame re = new RegistFrame();
re.setVisible(true);
this.UserName.setText("");
this.PassWord.setText("");
} else {
prop.setProperty(name, pwd);
//不存在时可以注册,即将输入框信息写入集合
Writer w = new FileWriter("user.txt");
prop.store(w, null);
w.close();
JOptionPane.showMessageDialog(this, "注册成功,请登录!");
dispose();
LoginFrame re = new LoginFrame();
re.setVisible(true);
}
} |
|