如何删除默认星号?

时间:2017-03-26 17:17:41

标签: java swing

在Netbeans中我已经在面板中放置了JpasswordField,你能告诉我如何删除JpasswordField中出现的默认星号吗?

1 个答案:

答案 0 :(得分:0)

要删除或更改显示的默认字符而不是字母,请使用JPasswordField#setEchoChar(char c)方法:

示例:

import java.awt.BorderLayout;
import javax.swing.*;

public class Example {

  public static void main(String args[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Box rowOne = Box.createHorizontalBox();
    rowOne.add(new JLabel("Username"));
    rowOne.add(new JTextField());

    Box rowTwo = Box.createHorizontalBox();
    rowTwo.add(new JLabel("Password"));
    JPasswordField jpassword = new JPasswordField();

    rowTwo.add(jpassword);
    f.add(rowOne, BorderLayout.NORTH);
    f.add(rowTwo, BorderLayout.SOUTH);
    f.setSize(300, 200);
    f.setVisible(true);

    jpassword.setEchoChar('#');

    System.out.println(jpassword.getEchoChar());

  }
}