java - 如何检查 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()
。
我认为在这里您应该验证输入的密码是否采用您需要的正确格式,而不是检查它是否已填写。
推荐阅读
- visual-studio - MSVC 2019:遗留库中的 libc 外部未解决
- python - 了解 Python 中多处理和线程之间的关键区别
- r - 当我在其中添加世界地图代码时,Flex Dashboard 无法正常工作
- javascript - 更干净的 Promise.all 语法?
- python - GNU Radio (Companion) 在变量替换时给出 Python 语法错误
- ios - 在 Xcode 中我得到错误:文件是 universai (2 sl�ces) 但不包含 x86_64 架构:
- javascript - 此 Javascript 代码中是否有错误?
- hsqldb - 在脚本中向 HSQLDB SqlTool 提供密码
- haskell - 不能以“读者”作为字段“强制”数据类型
- python - Django 管理员在发布后发送电子邮件