键绑定分开键检测

时间:2013-11-02 22:38:40

标签: java swing key-bindings

我遇到了键绑定问题:我编写了这个程序,但它只检测向下箭头。如何让它分别读取密钥?我做错了什么?

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class KeysExample extends JFrame{

    public static void main(final String args[]){
        final JFrame frame = new JFrame("Frame Key");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Action actionSpace = new AbstractAction(){
            public void actionPerformed(ActionEvent actionSpaceEvent){
                Component temporaryLostComponent = null;
                JOptionPane.showMessageDialog(temporaryLostComponent, "Pressed Spacebar");
            }
        };

        Action actionRight = new AbstractAction(){
            public void actionPerformed(ActionEvent actionRightEvent){
                Component temporaryLostComponent = null;
                JOptionPane.showMessageDialog(temporaryLostComponent, "Pressed Right Arrow");
            }
        };

        Action actionLeft = new AbstractAction(){
            public void actionPerformed(ActionEvent actionLeftEvent){
                Component temporaryLostComponent = null;
                JOptionPane.showMessageDialog(temporaryLostComponent, "Pressed Left Arrow");
            }
        };

        Action actionUp = new AbstractAction(){
            public void actionPerformed(ActionEvent actionUpEvent){
                Component temporaryLostComponent = null;
                JOptionPane.showMessageDialog(temporaryLostComponent, "Pressed Up Arrow");
            }
        };

        Action actionDown = new AbstractAction(){
            public void actionPerformed(ActionEvent actionDownEvent){
                Component temporaryLostComponent = null;
                JOptionPane.showMessageDialog(temporaryLostComponent, "Pressed Down Arrow");
            }
        };

        JPanel content = (JPanel) frame.getContentPane();
        KeyStroke space = KeyStroke.getKeyStroke("SPACE");
        KeyStroke right = KeyStroke.getKeyStroke("RightArrow");
        KeyStroke left = KeyStroke.getKeyStroke("LeftArrow");
        KeyStroke up = KeyStroke.getKeyStroke("UpArrow");
        KeyStroke down = KeyStroke.getKeyStroke("DownArrow");

        InputMap inputMap = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(space, "OPEN");
        inputMap.put(right, "OPEN");
        inputMap.put(left, "OPEN");
        inputMap.put(up, "OPEN");
        inputMap.put(down, "OPEN");
        content.getActionMap().put("OPEN", actionSpace);
        content.getActionMap().put("OPEN", actionRight);
        content.getActionMap().put("OPEN", actionLeft);
        content.getActionMap().put("OPEN", actionUp);
        content.getActionMap().put("OPEN", actionDown);

        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}

另外,我知道代码有点广泛,有些行可能是多余的。我是初学者,所以我还有很多东西需要学习。我也意识到我用JFrame创建的窗口中没有显示任何内容,这将在以后出现。知道我需要解决这个问题。

1 个答案:

答案 0 :(得分:3)

  

但它只检测向下箭头。

这是因为您将所有笔划分配给“OPEN”关键字,而分配给“OPEN”关键字的最后一个操作是downAction。

每个InputMap / ActionMap配对必须具有唯一的关键字名称(例如“UP”,“DOWN”,“RIGHT”......)。

KeyStroke.getKeyStroke("");

此外,你不能只为KeyStroke组成字符串。您必须使用KeyEvent类中的名称:

KeyEvent.VK_UP becomes "UP"
KeyEvent.VK_DOWN becomes "DOWN"

等。

阅读API以获取String的确切格式。

您可以查看Motion Using the Keyboard以获取更多信息和示例。