12.12.2 使用JPasswordField

12.12.2 使用JPasswordField

JPasswordFieldJTextField的一个子类,它是SwingMVC设计的产品:JPasswordFieldJTextField的各种特征几乎完全一样,只是当用户向JPasswordField输入内容时,JPasswordField并不会显示出用户输入的内容,而是以echo字符(通常是星号和黑点)来代替用户输入的所有字符
JPasswordFieldJTextField的用法几乎完全一样,连构造器的个数和参数都完全一样。但是JPasswordField多了一个setEchoChar(Char ch)方法,该方法用于设置该密码框的echo字符——当用户在密码输入框内输入时,每个字符都会使用该echo字符代替。
除此之外,JPasswordField重写了JTextComponentgetText()方法,并且不再推荐使用getText方法返回字符串密码框的字符串,因为getText方法所返回的字符串会一直停留在虚拟机中,直到垃圾回收,这可能导致存在一些安全隐患,所以**JPasswordField提供了一个getPassword()方法,该方法返回个字符数组,而不是返回字符串**,从而提供了更好的安全机制.
当程序使用完getPassword()方法返回的字符数组后,应该立即清空该字符数组的内容,以防该数组泄露密码信息.

JPasswordField类的方法 描述
void setEchoChar(char c) Sets the echo character for this JPasswordField.
char[] getPassword() Returns the text contained in this TextComponent.
String getText() Deprecated. As of Java 2 platform v1.2, replaced by getPassword.