Java KeyBindings: How does it work?

一个人想着一个人 提交于 2019-12-12 02:25:26

问题


I keep seeing that I should use Key Bindings instead of KeyListeners. So I found this page: Key Bindings. I read through it and tried to implement it.

    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);

I decided to just see what would happen. The class this is in extends a JPanel. Earlier, I used this.setFocusable(true). However, I don't see anything happen when I type '1'. What am I doing wrong? How are Key Bindings supposed to be implemented?


回答1:


Your example will only work IF the panel has focus, try using getInputMap(WHEN_IN_FOCUSED_WINDOW) instead.

You may also find that your KeyStroke statement won't work either, I'd suggest trying to use KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, true) instead, which will provide you with a KeyStroke object that will respond to key releases.

Updated with example

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();
        }
    }
}


来源:https://stackoverflow.com/questions/15753551/java-keybindings-how-does-it-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!