API中的getText()方法已过时,谁能举例说明一下关于getPassword()用法

解决方案 »

  1.   

        private static boolean isPasswordCorrect(char[] input) {
            boolean isCorrect = true;
            char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' };        if (input.length != correctPassword.length) {
                isCorrect = false;
            } else {
                isCorrect = Arrays.equals (input, correctPassword);
            }        //Zero out the password.
            Arrays.fill(correctPassword,'0');        return isCorrect;
        }public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();    if (OK.equals(cmd)) { //Process the password.
            char[] input = passwordField.getPassword();
            if (isPasswordCorrect(input)) {
                JOptionPane.showMessageDialog(controllingFrame,
                    "Success! You typed the right password.");
            } else {
                JOptionPane.showMessageDialog(controllingFrame,
                    "Invalid password. Try again.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            }        //Zero out the possible password, for security.
            Arrays.fill(input, '0');        passwordField.selectAll();
            resetFocus();
        } else ...//handle the Help button...
    }
      

  2.   

    String.valueOf(passwordField.getPassword())  ==getText()
      

  3.   

    getPassword() 是返回一个char[];
    为什么要求不用getText() 而要用getPassword()呢, 这是从安全性方面的考虑, 你得到这个char[]并使用以后,需要立即向这个char[]中填0, 毁尸灭迹; 这样系统中就没有保存密码的明文了,  这样就安全了;
    安全的用法是://JPasswordField p=new JPasswordField();
    char[] password=p.getPassword();
    //...  在这里使用密码
    Arrays.fill(password,'\0');如果不讲究,就直接这样用//JPasswordField p=new JPasswordField();
    String password=new String(p.getPassword());
      

  4.   

    为什么放心?public static String valueOf(char data[]) {
        return new String(data);
    }