如何给swing 控件增加快捷键呢?
场景:
当光标聚焦在password 文本框时,按Alt+Enter键,使username文本框聚焦.如何实现呢?
- /***
- * 按Alt+Enter时,用户名文本框聚焦
- * @param tf :密码输入框
- */
- private void addKeyListener22(JTextField tf){
- tf.addKeyListener(new KeyListener() {
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyReleased(KeyEvent e) {
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if ((e.getKeyCode() == KeyEvent.VK_ENTER)
- && (((InputEvent) e)
- .isAltDown())) {
- usernameTF.requestFocus();
- usernameTF.selectAll();
- }
- }
- });
- }
方法调用:
- final JTextField passwdTf01 = new JPasswordField(10);
- final JTextField passwdTf02 = new JTextField(10);
- passwordOnFocus(passwdTf02);
- addKeyListener22(passwdTf01);
- addKeyListener22(passwdTf02);
参考:
时间: 2024-10-03 16:45:16