如何设置Java默认按钮以对ENTER键_released_作出反应



在我的应用程序中,我使用默认按钮。我希望它在释放ENTER密钥时做出反应。当按下ENTER键时不会。

我从按钮的InputMap中删除了KeyStroke。但这对我不起作用。我该怎么做?


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class ButtonTest {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                buildFrame();
            }
        });
    }
    private static void buildFrame() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JButton button = new JButton(new AbstractAction("Button") {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("ButtonTest::actionPerformed: CALLED");
            }
        });
        JButton button2 = new JButton("Button 2");
        InputMap im = button.getInputMap();
        im.put(KeyStroke.getKeyStroke("ENTER"), "none");
        im.put(KeyStroke.getKeyStroke("released ENTER"), "released");
        f.setLayout(new GridBagLayout());
        f.add(button);
        f.add(button2);
        f.getRootPane().setDefaultButton(button);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

这是示例代码。

JRootPane rootPane = SwingUtilities.getRootPane(/* Your JButton  */); 
rootPane.setDefaultButton(/* Your JButton  */);

默认按钮(即,在输入时触发的按钮,无论哪个组件是focusOwner)的键绑定在rootPane的componentInputMap中,即其类型为WHEN_in_FOCUSED_WINDOW的inputMap。所以从技术上讲,这就是调整的地方:

JComponent content = new JPanel();
content.add(new JTextField("some focusable"));
content.add(new JTextField("something else"));
JXFrame frame = wrapInFrame(content, "default button on released");
Action action = new AbstractAction("do something") {
    @Override
    public void actionPerformed(ActionEvent e) {
        LOG.info("clicked");
    }
};
JButton button = new JButton(action);
content.add(button);
frame.getRootPane().setDefaultButton(button);
// remove the binding for pressed
frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
    .put(KeyStroke.getKeyStroke("ENTER"), "none");
// retarget the binding for released
frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
    .put(KeyStroke.getKeyStroke("released ENTER"), "press");

注意:这与非默认按钮的按下/释放行为仍然不同,因为rootPane操作只需调用按钮即可。单击按钮而不需要经过武装/按下按钮Model的动作即可间接触发该操作。

    InputMap im = button.getInputMap();
    im.put(KeyStroke.getKeyStroke("ENTER"), "pressed");
    im.put(KeyStroke.getKeyStroke("released ENTER"), "released");

这样做的效果是,当按下ENTER键时,按钮只执行一次操作(如果我理解正确,这就是你想要的)。

EDIT:运行的以下代码表明,actionPerformed仅在ENTER释放时执行(尽管在ENTER按下时按钮已被按下)

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class ButtonTest {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                buildFrame();
            }
        });
    }
    private static void buildFrame() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JButton button = new JButton(new AbstractAction("Button") {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("ButtonTest::actionPerformed: CALLED");
            }
        });
        InputMap im = button.getInputMap();
        im.put(KeyStroke.getKeyStroke("ENTER"), "pressed");
        im.put(KeyStroke.getKeyStroke("released ENTER"), "released");
        f.add(button);
        f.getRootPane().setDefaultButton(button);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

您可以使用getKeyStroke()的版本,该版本允许您将onKeyRelease设置为true,就像在这个答案中一样

编辑:你可以停止重复,就像这个答案一样。

相关内容

  • 没有找到相关文章

最新更新