【问题标题】:How to check if a JPasswordField is NOT empty?如何检查 JPasswordField 是否为空?
【发布时间】:2021-08-14 10:45:32
【问题描述】:

这段代码的反义词是什么 --> 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.");
    }
  }
}

【问题讨论】:

  • == 相反是 !=。但是您应该研究以下方法: isEmpty() 。 myValue.trim().isEmpty(),例如
  • 检查使用password.getPassword().length > 0或!password.getPassword().isEmpty()

标签: java swing validation jpasswordfield


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-11-15
    • 2020-05-29
    • 2018-04-02
    • 2016-04-05
    • 2018-01-08
    • 2020-09-25
    • 2012-01-15
    • 2016-07-16
    • 2021-10-14
    相关资源
    最近更新 更多