0

我一直看到我应该使用 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”时,我看不到任何事情发生。我究竟做错了什么?应该如何实现键绑定?

4

1 回答 1

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();
        }
    }
}
于 2013-04-01T23:20:38.183 回答