如何使用密码字段

原文: https://docs.oracle.com/javase/tutorial/uiswing/components/passwordfield.html

JPasswordField 类是JTextField的子类,为密码输入提供专门的文本字段。出于安全原因,密码字段不显示用户键入的字符。相反,该字段显示的字符与输入的字符不同,例如星号’*’。作为另一种安全预防措施,密码字段将其值存储为字符数组,而不是字符串。与普通的文本字段一样,当用户指示文本输入完成时,密码字段会触发动作事件,例如按 Enter 按钮。

这是一个演示图片,打开一个小窗口并提示用户输入密码。

A snapshot of PasswordDemo, which uses a password field

单击“启动”按钮以使用 Java™Web Start下载 JDK 7 或更高版本)运行 PasswordDemo。或者,要自己编译并运行示例,请参考示例索引

Launches the PasswordDemo Application

密码是“bugaboo”。密码“bugaboo”仅为示例。在生产系统中使用安全认证方法。您可以在 PasswordDemo.java中找到该程序的完整代码。以下是创建和设置密码字段的代码:

  1. passwordField = new JPasswordField(10);
  2. passwordField.setActionCommand(OK);
  3. passwordField.addActionListener(this);

传递给JPasswordField构造器的参数表示字段的首选大小,在这种情况下至少为 10 列宽。默认情况下,密码字段为每个键入的字符显示一个点。如果要更改回音字符,请调用setEchoChar方法。然后,代码将一个动作监听器添加到密码字段,该字段检查用户键入的值。以下是动作监听器的actionPerformed方法的实现:

  1. public void actionPerformed(ActionEvent e) {
  2. String cmd = e.getActionCommand();
  3. if (OK.equals(cmd)) { //Process the password.
  4. char[] input = passwordField.getPassword();
  5. if (isPasswordCorrect(input)) {
  6. JOptionPane.showMessageDialog(controllingFrame,
  7. "Success! You typed the right password.");
  8. } else {
  9. JOptionPane.showMessageDialog(controllingFrame,
  10. "Invalid password. Try again.",
  11. "Error Message",
  12. JOptionPane.ERROR_MESSAGE);
  13. }
  14. //Zero out the possible password, for security.
  15. Arrays.fill(input, '0');
  16. passwordField.selectAll();
  17. resetFocus();
  18. } else ...//handle the Help button...
  19. }

Security note: To further enhance security, once you are finished with the character array returned by the getPassword method, you should set each of its elements to zero. The preceding code snippet shows how to do this.


使用密码字段的程序通常在完成任何需要密码的操作之前验证密码。该程序调用一个私有方法isPasswordCorrect,它将getPassword方法返回的值与存储在字符数组中的值进行比较。这是它的代码:

  1. private static boolean isPasswordCorrect(char[] input) {
  2. boolean isCorrect = true;
  3. char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' };
  4. if (input.length != correctPassword.length) {
  5. isCorrect = false;
  6. } else {
  7. isCorrect = Arrays.equals (input, correctPassword);
  8. }
  9. //Zero out the password.
  10. Arrays.fill(correctPassword,'0');
  11. return isCorrect;
  12. }

下表列出了常用的JPasswordField构造器和方法。有关密码字段继承的 API 的信息,请参阅如何使用文本字段

构造器或方法 目的
JPasswordField()

JPasswordField(String) JPasswordField(String,int) JPasswordField(int) JPasswordField(Document,String,int) | 创建密码字段。如果存在,int参数指定列中所需的宽度。 String参数包含字段的初始文本。 Document参数为字段提供自定义模型。 | | char [] getPassword() | 以字符数组的形式返回密码。 | | void setEchoChar(char) char getEchoChar() | 设置或获取显示的 echo 字符,而不是用户键入的实际字符。 | | void addActionListener(ActionListener) void removeActionListener(ActionListener) (在JTextField中定义) | 添加或删除动作监听器。 | | void selectAll() (在JTextComponent中定义) | 选择密码字段中的所有字符。 |

PasswordDemo 是 Tutorial 唯一使用JPasswordField对象的示例。但是,教程中有许多使用JTextField对象的示例,其 API 由JPasswordField继承。有关详细信息,请参阅使用文本字段的示例

如果您使用 JavaFX 进行编程,请参阅密码字段