Replace keyAdapter with key binding

2019-06-28 06:01发布

How could I use key bindings in this code as key adapter doesn't work well? I already looked at the documentation of key binding but I can't understand how to do this.

private class KeyLis extends KeyAdapter 
{   
    @Override
    public void keyPressed(KeyEvent e) 
    {
        switch (e.getKeyCode())
        {
        case KeyEvent.VK_UP:
            up = true;
            break;
        case KeyEvent.VK_DOWN:
            down = true;
            break;
        case KeyEvent.VK_LEFT:
            left = true;
            break;
        case KeyEvent.VK_RIGHT:
            right = true;
            break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) 
    {
        switch (e.getKeyCode())
        {
        case KeyEvent.VK_UP:
            up = false;
            break;
        case KeyEvent.VK_DOWN:
            down = false;
            break;
        case KeyEvent.VK_LEFT:
            left = false;
            break;
        case KeyEvent.VK_RIGHT:
            right = false;
            break;
        }
    }
}

Thank you

1条回答
何必那么认真
2楼-- · 2019-06-28 06:17

You need to add the respective key to the InputMap of the component on which to apply KeyBinding as shown here :

panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    KeyStroke.getKeyStroke("pressed UP"), "pressedUPAction");
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    KeyStroke.getKeyStroke("released UP"), "releasedUPAction");

panel.getActionMap().put("pressedUPAction", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        System.out.println("UP Arrow Pressed");
    }
});

panel.getActionMap().put("releasedUPAction", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        System.out.println("UP Arrow Released");
    }
});

Have a look at this working example :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SmallExample {

    private JButton button;
    private JPanel panel;

    private void displayGUI() {
        JFrame frame = new JFrame("Small Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        panel = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return (new Dimension(100, 100));
            }
        };
        panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke("pressed UP"), "pressedUPAction");
        panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke("released UP"), "releasedUPAction");

        panel.getActionMap().put("pressedUPAction", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.out.println("UP Arrow Pressed");
            }
        });

        panel.getActionMap().put("releasedUPAction", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.out.println("UP Arrow Released");
            }
        });

        frame.setContentPane(panel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new SmallExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}
查看更多
登录 后发表回答