繁体   English   中英

Java JPasswordField

[英]Java JPasswordField

因此,我必须编写一个程序来提示用户输入密码,该密码必须符合三个要求:至少8个字符长,仅字母和数字以及至少两位数字。 现在,我创建的用于检查这三个要求的方法很合理,但是我的程序还必须执行一些异常处理,并且如果其中一项要求已关闭,则能够要求用户重新输入密码,并显示相应的错误消息到符合每个要求。 我创建了一个字符串errorMessage来中继该消息,但是当我尝试在我的main中调用它时却给了我一个错误?

我的另一个问题是必须使用JPasswordField将密码输入到我的程序中,但是由于我还要阅读许多其他因素,例如JPanel,按钮和动作事件,因此我甚至难以设置密码。 我尝试使用JPasswordField并注意到,输入密码的行将其作为数组接收,当我的checkPassword方法需要一个字符串时,我该如何将该密码作为字符串输入?

到目前为止,这是我程序的内容:

   import java.awt.event.ActionListener;
   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JOptionPane;
   import javax.swing.JPasswordField;

   import javafx.event.ActionEvent;

public class Ed10Chp6Ex6Point18CheckPasswordProgram {

public static void main(String[] args) {

    final JFrame frame = new JFrame("Check Password Program");
    JLabel jlbPassword = new JLabel("Please enter the password: ");
    JPasswordField jpwName = new JPasswordField(15);
    jpwName.setEchoChar('*');
    jpwName.addActionListener(new ActionListener()) {

        public void actionPerformed(ActionEvent e) {
            JPasswordField input = (JPasswordField) e.getSource();
            char[] password = input.getPassword();

            if(checkPassword(password)){
                JOptionPane.showMessageDialog(frame, "Congradulations, your password follows all the requirements");
            }
            else{
                JOptionPane.showMessageDialog(frame, errorMessage);
            }
        }
    }
}

public static boolean checkPassword(String x) {

    String errorMessage = "";

    //must have at least eight characters
    if (x.length() < 8){
      errorMessage = "The password you entered is invalid, password must be at least 8 characters";
        return false;
    }

    //consists of only letters and digits
    for (int i = 0; i < x.length(); i++) {
      if (!Character.isLetter(x.charAt(i)) && !Character.isDigit(x.charAt(i))) {
          errorMessage = "The password you entered is invalid, password must contain only letters and digits";
        return false;
      }
    }

    //must contain at least two digits
    int count = 0;
    for (int i = 0; i < x.length(); i++) {
      if (Character.isDigit(x.charAt(i))){
        count++;
      }
    }

    if (count >= 2){
      return true;
    }
    else {
        errorMessage = "The password you entered is invalid, password must contain at least two digits";
      return false;
    }
}
}

如果我的某些问题很初级,我深表歉意,任何帮助将不胜感激,谢谢!

我该如何以密码代替字符串

使用checkPassword(new String(input.getPassword))或更新您的方法以接受char[]而不是String。

对于错误检查,例如,在实现类似ShortPasswordException extends Exception的类ShortPasswordException extends Exception之后,应该使用throw new ShortPasswordException() ,在该位置要引发该错误。

那你就可以

try { 
    checkPassword();
} catch (ShortPasswordException e) { 
    // TODO: Handle exception
}

冒险的提示:使用正则表达式检查密码要求。 例如,匹配\\d{2}表示您有2个连续数字。

马上有两件事:

(1)确保要导入正确的类,不要依赖IDE进行正确的导入。 您正在从JavaFX导入ActionEvent类,但是要使用的框架是Swing。

更改

import javafx.event.ActionEvent;

import java.awt.event.ActionEvent;

我对JavaFX和Swing相互之间如何协同工作不太熟悉,但是使用正确的类通常可以避免麻烦和编译/运行时错误。

(2) java.lang.String类中的静态方法提供了一种将char数组转换为字符串的便捷方法。 在您的actionPerformed方法中,添加以下内容:

String passStr = String.valueOf(password);

例如

@Override
public void actionPerformed(ActionEvent e) {
    // Get the source of the event, we know it is a JPasswordField so we cast it.
    JPasswordField input = (JPasswordField) e.getSource();
    // Get the char array from the input the user typed stored in the input object.
    char[] password = input.getPassword();
    // Convert char[] to String
    String passwordStr = String.valueOf(password);
    if(checkPassword(passwordStr)){
        JOptionPane.showMessageDialog(frame, "Congradulations, your password follows all the requirements");
    } else {
        JOptionPane.showMessageDialog(frame, errorMessage);
    }
}

暂无
暂无

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

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