如何检查 JPasswordField 是否不为空?

问题描述

这段代码的反面是什么 --> password.getpassword().length == 0

就像检查它是否填满而不是空。

JButton confirm = new JButton("Sign Up");
confirm.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
    UserInfo a = new UserInfo();

    /*here is my problem I want it to Check if someone inputted a password so I can 
                proceed to open the second frame */


    if(!firstname.getText().trim().isEmpty() && !lastname.getText().trim().isEmpty() &&  password.getpassword().length == 0 && pass2.getpassword().length == 0
        && !address.getText().trim().isEmpty() )
    
    a.frame2.setVisible(true);
    frame.dispose(); 
      
    
    if(firstname.getText().trim().isEmpty()) {
      missingfirst.setText("Please Add Your First Name.");
  
    }
    if(lastname.getText().trim().isEmpty()) {
      missinglast.setText("Please Add Your Last Name.");
    }
    if(password.getpassword().length == 0) {
      missingpass.setText("Please Add A Password.");
    }
    if(pass2.getpassword().length == 0) {
      missingrepass.setText("Please Re-Type Your Password.");
    }
    if(address.getText().trim().isEmpty()) {
      missingadd.setText("Please Enter An Address.");
    }
    
    else if (!(password.getpassword().equals(pass2.getpassword()))) {
      missingrepass.setText("Password Doesn't Match.");
    }
  }
}

解决方法

使用 password.getPassword().length > 0!password.getPassword().isEmpty() 检查。

我认为在这里您应该验证输入的密码是否为您要求的正确格式,而不是检查是否已填写。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...