Java KeyBindings:它是如何工作的?

时间:2013-04-01 23:17:30

标签: java key-bindings

我一直看到我应该使用Key Bindings而不是KeyListeners。所以我找到了这个页面:Key Bindings。我仔细阅读并尝试实施它。

    Action numPressed = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("hi");
        }
    };
    this.getInputMap().put(KeyStroke.getKeyStroke("1"), "released");
    this.getActionMap().put("released", numPressed);

我决定看看会发生什么。这个类扩展了一个JPanel。早些时候,我使用了this.setFocusable(true)。但是,当我输入'1'时,我没有看到任何事情发生。我究竟做错了什么?如何实现密钥绑定?

1 个答案:

答案 0 :(得分:3)

您的示例仅在面板具有焦点时才起作用,请尝试使用getInputMap(WHEN_IN_FOCUSED_WINDOW)

您可能还会发现您的KeyStroke语句也不起作用,我建议您尝试使用KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, true),这会为您提供一个KeyStroke对象,它会响应关键版本。

更新了示例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestKeyBindings04 {

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

    public TestKeyBindings04() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, false), "pressed");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, true), "released");

            am.put("pressed", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Pressed");
                }
            });

            am.put("released", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("released");
                }
            });

            setFocusable(true);
            requestFocusInWindow();        
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }
    }
}