简体   繁体   English

JoptionPane验证

[英]JoptionPane Validation

I have a Swing GUI where I am restricting the user registration so that the username and the password cannot be the same. 我有一个Swing GUI,我限制用户注册,以便用户名和密码不能相同。 I am using JoptionPane for the task with the following code: 我使用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);
    }
...

The problem is that I had to use System.exit(0) ; 问题是我必须使用System.exit(0) ; without it, the next code was getting executed. 没有它,下一个代码就会被执行。 Even after the JOptionPane poped up, the registration was succeeding. 即使在JOptionPane加速之后,注册也成功了。 I do not need the system to exit, but I need the user to be kept on the registration page after the validation. 我不需要系统退出,但我需要在验证后将用户保留在注册页面上。 What is the best way to do this? 做这个的最好方式是什么? Is there other convenient ways of doing this rather than using the JOptionPane ? 有没有其他方便的方法来做这个而不是使用JOptionPane

Replace 更换

System.exit(0);

with

return;

if you do not want the rest of the method to be performed 如果您不希望执行该方法的其余部分

You need to place your code within endless loop, and break it upon successful result. 您需要将代码置于无限循环中,并在成功结果时将其分解。 Something like: 就像是:

while(true)
{
    // get input from user

    if(vlaidInput) break;
}

place that next code into else part may be it works 把下一个代码放到else部分可能是有效的

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
}

First of all, it is best if the UI and business logic (in this case, validation) are separated. 首先,最好将UI和业务逻辑(在本例中为验证)分开。 Have them separate sort of suggest a better way of handling interaction on its own. 让它们分开建议一种更好的方式来处理交互。 Thus, it makes sense to create a separate class UserValidation with method boolean isValid() . 因此,使用方法boolean isValid()创建单独的类UserValidation是有意义的。 Something like this: 像这样的东西:

public class UserValidation {
    private final String name;
    private final String passwd;
    private final String passwdRpt;

    public UserValidation(final String name, final String passwd, final String passwdRpt) {
        this.name = name;
        this.passwd = passwd;
        this.passwdRpt = passwdRpt;
    }

    public boolean isValid() {
       // do your validation logic and return true if successful, or false otherwise
    }
}

Then the action code would look like this: 然后动作代码如下所示:

public void actionPerformed(ActionEvent e) {
        if (new UserValidation(tuser.getText(), pass1.getText(), pass2.getText()).isValid()) {
           // do everything needed is validation passes, which should include closing of the frame of dialog used for entering credentials.
        }

        // else update the UI with appropriate error message -- the dialog would not close by itself and would keep prompting user for a valid entry
}

The suggested approach gives you a way to easily unit test the validation logic and use it in different situations. 建议的方法为您提供了一种轻松单元测试验证逻辑并在不同情况下使用它的方法。 Please also note that if the logic in method isValid() is heavy than it should be executed by a SwingWorker . 还请注意,如果方法isValid()的逻辑比SwingWorker应该执行的要大。 The invocation of SwingWorker is the responsibility of the action (ie UI) logic. SwingWorker的调用是动作(即UI)逻辑的责任。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM