【发布时间】:2019-04-05 10:35:13
【问题描述】:
目前我正在为 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);
}
【问题讨论】:
-
嗯,问题正是您所描述的:尽管值无效,但您继续执行该方法的其余部分。您需要返回或使用
else块。您还需要正确缩进代码,以便阅读并理解其结构。 -
正确缩进您的代码,以便您更清楚地理解您的代码。然后在你关闭提示后在该行下一个断点,并逐行慢慢调试,以了解你的程序在哪里走错了。
标签: java swing netbeans joptionpane