Is there an alternative to JPasswordField?(有没有 JPasswordField 的替代品?)
问题描述
输入密码时
yeast bulk seize is shows pain
每个人都可以听到敲击空格键的声音,因此在密码字段中显示空格似乎也是合乎逻辑的.所以我想要一些能够展示的东西
everybody can hear tapping the space bar, so it seems logical to display the spaces in the password field, too. So I'd like something capable of showing
***** **** ***** ** ***** ****
而不是
******************************
这将使打字更容易,同时几乎不会降低安全性.
This would make typing easier while hardly decreasing the security.
在更新 Riduidel 的评论之前请三思.当 Bruce Schneier 写道 "是时候以明文显示大多数密码",然后显示其中的一小部分也必须是正确的.尤其是显示一个可以通过聆听就可以捕捉到的部分.
Think twice before you update Riduidel's comment. When Bruce Schneier writes "It's time to show most passwords in clear text", then showing a small part of it must be correct, too. Especially showing a part which may get captured simply by listening.
推荐答案
这是一个使用 setEchoChar()
使密码在预定义的时间内可见:例如三秒.
Here's a variation that uses setEchoChar()
to make the password visible for a predefined time: three seconds for example.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.Timer;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
/** @see http://stackoverflow.com/questions/5339702 */
public class PasswordTest {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
private static void createAndShowGui() {
JFrame jf = new JFrame("Test Password");
JPasswordField jpwd = new JPasswordField();
TimedPasswordListener tpl = new TimedPasswordListener(jpwd);
jpwd.getDocument().addDocumentListener(tpl);
jf.add(jpwd);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.pack();
jf.setVisible(true);
}
}
class TimedPasswordListener implements DocumentListener, ActionListener {
private Timer timer = new Timer(3000, this);
private char echoChar;
private JPasswordField pwf;
public TimedPasswordListener(JPasswordField jp) {
pwf = jp;
timer.setRepeats(false);
}
public void insertUpdate(DocumentEvent e) {
showText(e);
}
public void removeUpdate(DocumentEvent e) {
showText(e);
}
public void changedUpdate(DocumentEvent e) {}
public void showText(DocumentEvent e) {
if (0 != pwf.getEchoChar()) {
echoChar = pwf.getEchoChar();
}
pwf.setEchoChar((char) 0);
timer.restart();
}
public void actionPerformed(ActionEvent e) {
pwf.setEchoChar(echoChar);
}
}
这篇关于有没有 JPasswordField 的替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有 JPasswordField 的替代品?
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01