我有一个Swing GUI,我在其中限制用户注册,以便用户名和密码不能相同。我使用JoptionPane来完成这个任务,代码如下:
public void actionPerformed(ActionEvent e) {
String username = tuser.getText();
String password1 = pass1.getText();
String password2 = pass2.getText();
String workclass = wclass.getText();
Connection conn = null;
try {
if(username.equalsIgnoreCase(password1)) {
JOptionPane.showMessageDialog(null,
"Username and Password Cannot be the same. Click OK to Continue",
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
...问题是我必须使用System.exit(0);如果没有它,下一段代码就会被执行。即使在JOptionPane弹出之后,注册仍然成功。我不需要系统退出,但我需要用户被保留在注册页面后的验证。做这件事最好的方法是什么?除了使用JOptionPane,还有没有其他方便的方法可以做到这一点
发布于 2012-07-31 17:12:50
替换
System.exit(0);使用
return;如果您不希望执行方法的其余部分
发布于 2012-07-31 17:13:59
你需要把你的代码放在无限循环中,并在成功结果时中断它。类似于:
while(true)
{
// get input from user
if(vlaidInput) break;
}发布于 2012-07-31 17:26:29
将下一段代码放到其他部分中可能是因为它工作正常
if(username.equalsIgnoreCase(password1))
{
JOptionPane.showMessageDialog(null, "Username and Password Cannot be the same. Click OK to Continue","Error",JOptionPane.ERROR_MESSAGE);
}
else
{
//place that next code
// username and password not equals, then it will execute the code
}https://stackoverflow.com/questions/11736695
复制相似问题