【问题标题】:How to get a KeyStroke working for a JLabel like it does for JButton?如何让 KeyStroke 像 JButton 一样为 JLabel 工作?
【发布时间】:2021-08-30 04:23:23
【问题描述】:

我有一个像这张图片一样的登录表单。

登录按钮是一个带有图标的 JLabel。我希望能够按“ENTER”并登录。

我已将登录逻辑放入一个名为 doAction(); 的方法中;我在登录标签上有一个 MouseEvent,它按预期工作,并在我单击登录 Jlabel 时调用 doAction()。我希望每次按“ENTER”时都会发生同样的事情。我试过这个:

        KeyStroke keyStroke = KeyStroke.getKeyStroke("ENTER");
        int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
        InputMap inputMap = login.getInputMap(condition);
        ActionMap actionMap = login.getActionMap();
        inputMap.put(keyStroke, keyStroke.toString());
        actionMap.put(keyStroke.toString(), new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                doAction();
            }
        });

问题是只有当我在密码字段中使用光标时这才有效。如果我在 usernameField 中使用光标,则它不起作用

【问题讨论】:

标签: java swing key-bindings keyevent


【解决方案1】:

很难说“确切”发生了什么,因为我们只有一个很小的、断章取义的 sn-p,但我倾向于避免使用 KeyStroke.getKeyStroke("ENTER"),因为它并不总是产生预期的结果。相反,我直接使用KeyEvent 虚拟键。

例如

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            add(new JTextField(10), gbc);
            add(new JPasswordField(10), gbc);

            add(new JLabel("..."), gbc);

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Login");
            am.put("Login", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    System.out.println("Do stuff here");
                }
            });
        }

    }
}

【讨论】:

  • 我刚刚按照你说的做了,它也做了同样的事情。仅当我在密码字段中使用光标时才有效
  • @DanielRad 我提供的示例对我来说效果很好 - 如果它不适合您,那么您的代码还有其他问题,您需要提供minimal reproducible example跨度>
猜你喜欢
  • 2019-09-09
  • 1970-01-01
  • 2011-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多