在JTextFIeld(Java Swing)中隐藏/显示密码 [英] Hide/Show Password in a JTextFIeld (Java Swing)
问题描述
所以我一直在研究密码强度检查器,它的工作方式是用户在文本字段中输入一些随机文本,然后显示瞬时视觉反馈(点数分解)。我还添加了一个复选框,在选中时,应隐藏密码,即用星号替换所有字符,同时保留用户输入的实际文本。正在使用文档侦听器来跟踪文本字段内的更改。 (每个条目上的字符都会被分析然后得分)
So I've been working on a Password Strength Checker and the way it works is that the user enters some random text into a textfield and then instantaneous visual feedback (breakdown of points) is displayed. I've also added a checkbox, which on being selected, should hide the password i.e. replace all the chars with asterisks, while preserving the actual text input by the user. A document listener is being used to keep track of changes inside the textfield. (each char on entry gets analyzed and then scored)
所以,我的问题是,我究竟如何用星号掩盖用户输入并保留其原始值?
So, my question is, how exactly do I mask the user input with asterisks preserving its original value?
以下是GUI的样子:
http://speedcap.net/sharing/screen.php?id=files/51/2f/512f9abb3f92a25add7c593e9d80e9e4.png
推荐答案
我如何使用保留原始值的星号来屏蔽用户输入?
How exactly do I mask the user input with asterisks preserving its original value?
使用 JPasswordField
其功能很好 jPasswordField.getPassword();
获取密码 char []
使用。
Use the JPasswordField
which has nice function jPasswordField.getPassword();
to get the password as char[]
to work with.
- 使用
jPasswordField1.setEchoChar('*')
来掩盖密码字符*
。 - 如果您希望看到要插入的值,请使用
jPasswordField1.setEchoChar((char)0);
设置值0
表示您希望看到输入的文本,类似于标准JTextField
的行为。
- Use
jPasswordField1.setEchoChar('*')
to mask the password characters with*
. - If you wish to see the value you are inserting use
jPasswordField1.setEchoChar((char)0);
Setting a value of0
indicates that you wish to see the text as it is typed, similar to the behavior of a standardJTextField
.
教程和参考:
- How to use Password Fields
- setEchoChar(char)
这篇关于在JTextFIeld(Java Swing)中隐藏/显示密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!