目前我正在研究java netbeans的代码来创建一个正常运行的登录表单。我的代码运行完美,但唯一的问题是我的验证不按照我希望的方式工作。例如,当我离开表单的用户名文本字段时。它向我显示输入凭据的提示。但是一旦我按下那个提示就解雇了。它使用空文本字段提交表单。这是我的代码如下:
我究竟做错了什么?
try{
String username;
username = usernameTxt.getText();
String password;
password= new String (passwordTxt.getText());
if(username.equals("")){
JOptionPane.showMessageDialog(null,"Please Enter Your Username.");
}
if(password.equals("")){
JOptionPane.showMessageDialog(null,"Please Enter Your Password.");
}
String confirmpassword = new String (confirmpassTxt.getText());
if(password.equals(confirmpassword)){
confirmpassLab.setText("");
}
else {
confirmpassLab.setText("PASSWORD DOES NOT MATCH.");
}
PrintWriter pw = new PrintWriter (new BufferedWriter(new FileWriter("ResidentLoginCredentials.txt",true))); // TRUE is to get all the information from the form to send it into the database. this code is to send the information to the databse
pw.println(username); // passing that value to the file
pw.println(password); // passing the name value to the database
pw.close();
JOptionPane.showMessageDialog(this,"Your Account Has Been Successfully Registered.","File Message", JOptionPane.INFORMATION_MESSAGE);
usernameTxt.setText("");
passwordTxt.setText("");
this.setVisible(false); // this makes the current form disappear and the new form to open
residentassignunit ru = new residentassignunit(); // this codes are to redirect user to another form
ru.setVisible(true);
}
catch(IOException e)
{
JOptionPane.showMessageDialog(this,"File Not Found","File Error Message Box",JOptionPane.ERROR_MESSAGE);
}
try{
String username;
username = usernameTxt.getText();
String password;
password= new String (passwordTxt.getText());
if(username.equals("")){
JOptionPane.showMessageDialog(null,"Please Enter Your Username.");
}else if(password.equals("")){
JOptionPane.showMessageDialog(null,"Please Enter Your Password.");
}else if(!password.equals(confirmpassword)){
confirmpassLab.setText("PASSWORD DOES NOT MATCH.");
}else{
String confirmpassword = "";
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("ResidentLoginCredentials.txt",true))); // TRUE is to get all the information from the form to send it into the database.this code is to send the information to the databse
pw.println(username); // passing that value to the file
pw.println(password); // passing the name value to the database
pw.close();
JOptionPane.showMessageDialog(this,"Your Account Has Been Successfully Registered.","File Message", JOptionPane.INFORMATION_MESSAGE);
usernameTxt.setText("");
passwordTxt.setText("");
this.setVisible(false); // this makes the current form disappear and the new form to open
residentassignunit ru = new residentassignunit(); // this codes are to redirect user to another form
ru.setVisible(true);
}
}
catch(IOException e)
{
JOptionPane.showMessageDialog(this,"File Not Found","File Error Message Box",JOptionPane.ERROR_MESSAGE);
}