【发布时间】:2012-03-07 22:54:22
【问题描述】:
您能帮我如何使用KeyBinding 和Consume 一起输入Chars,就像使用KeyListener 演示我的SSCCE 一样
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class Login {
private static final long serialVersionUID = 1L;
/* PassWord for unlock*/
private PswChecker checker = new PswChecker("pass");
public Login() {
JTextField firstField = new JTextField(10);
firstField.addKeyListener(passwordKeyListener);
JLabel firstLabel = new JLabel("Password is 'pass' ", JLabel.RIGHT);
firstLabel.setLabelFor(firstField);
JPanel p = new JPanel();
p.setLayout(new GridLayout(0, 2, 5, 5));
p.add(firstLabel);
p.add(firstField);
JFrame f = new JFrame("login");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setContentPane(p);
f.setLocationByPlatform(true);
f.pack();
f.setVisible(true);
}
//
private KeyListener passwordKeyListener = new KeyListener() {
private boolean enabled = true;
@Override
public void keyTyped(KeyEvent e) {
if (!enabled) {
return;
}
if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
boolean b = checker.accept(e.getKeyChar());
e.consume();
if (b) {
enabled = false;
if (e.getComponent() != null) {
e.getComponent().removeKeyListener(this);
}
unlock();
}
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
};
void unlock() {
JOptionPane.showMessageDialog(null, "unlocked");
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Login log = new Login();
}
});
}
class PswChecker {
private String password = null;
private boolean unlocked = false;
private long lastInputTimestamp = 0L;
private int index = 0;
public PswChecker(String password) {
if (password == null) {
throw new IllegalArgumentException("Null password");
}
if (password.trim().length() == 0) {
throw new IllegalArgumentException("Empty password");
}
this.password = password;
}
public boolean accept(char c) {
if (unlocked) {
return true;
}
long timestamp = System.currentTimeMillis();
if (timestamp - lastInputTimestamp > 700) {
index = 0;
}
lastInputTimestamp = timestamp;
if (password.charAt(index) == c) {
index++;
} else {
if (password.charAt(0) == c) {
index = 1;
} else {
index = 0;
}
}
unlocked = (index == password.length());
return unlocked;
}
public boolean isUnlocked() {
return unlocked;
}
public boolean isLocked() {
return !unlocked;
}
@Override
public String toString() {
return unlocked ? "UNLOCKED" : "LOCKED";
}
/*private boolean check(String keystrokes, String password, boolean expectUnLocked) {
PswChecker checker = new PswChecker(password);
for (int i = 0; i < keystrokes.length(); i++) {
checker.accept(keystrokes.charAt(i));
}
return checker.isUnlocked();
}*/
}
}
【问题讨论】:
-
+1 这只是一个例子,还是应该使用
JPasswordField? -
@trashgod 对,我的问题不是关于 JPasswordField,而是关于使用 KeyBindings 中的事件,
-
如果不是密码,还有什么?你到底想实现什么?
-
对,这与密码无关,
1)我不想使用(也许我知道如何使用)DocumentListener或DocumentFilter,2) 想听KeyBindings进入JTextComponent并确定(在这种情况下,此逻辑是否被转换为密码检查器)是一些Chars类型并覆盖event.consume()以输出到View。3)现在我只能将KeyListener用于event.consume(), -
为什么? (对不起,近乎大喊,这 15 个字母的限制是...... :)
标签: java swing key-bindings keylistener