【问题标题】:Java JPasswordFieldJava JPasswordField
【发布时间】:2016-07-21 23:43:12
【问题描述】:

所以我必须编写一个提示用户输入密码的程序,该密码必须遵循三个要求:至少 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;
    }
}
}

如果我的一些问题看起来很初步,我提前道歉,任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 你试过String pass = new String(password);
  • 哦,哇,谢谢!把它放在 char[] 密码行之后,错误就消失了。你会知道为什么调用 errorMessage 会出错吗?
  • errorMessage 没有定义在JOptionPane.showMessageDialog(frame, errorMessage);的范围内
  • 因为 errorMessage 是您的 checkPassword 方法中的局部变量。您无法从 main 方法中访问它。此外,您说您必须处理例外情况。不要让你的 checkPassword 方法返回一个布尔值,而是让它抛出一个检查异常并在你的 main 方法中捕获它。
  • 任何人都可以评论我在尝试使用 JPasswordField 时出现的问题/缺失,还是我做得正确?是否会弹出一个对话框窗口,让用户使用我编写的代码输入密码?

标签: java jpasswordfield


【解决方案1】:

两件事情马上发生:

(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);
    }
}

【讨论】:

    【解决方案2】:

    我怎样才能把那个密码当作一个字符串来代替

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

    至于错误检查,你应该使用throw new ShortPasswordException(),在你实现了一个像ShortPasswordException extends Exception这样的类之后,你想抛出那个错误,例如。

    那么,你就可以了

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

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

    【讨论】:

      猜你喜欢
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 2019-05-25
      相关资源
      最近更新 更多