将keyAdapter替换为键绑定 [英] Replace keyAdapter with key binding

查看:104
本文介绍了将keyAdapter替换为键绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于密钥适配器无法正常工作,我如何在此代码中使用密钥绑定?我已经看过键绑定的文档,但是我不明白该怎么做.

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

谢谢

推荐答案

您需要将相应的密钥添加到要在其上应用KeyBinding的组件的InputMap中,如下所示:

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

看看这个工作示例:

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

这篇关于将keyAdapter替换为键绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆