目前,我正在为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);
}
答案 0 :(得分:0)
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);
}