有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何确定通过输入/ActionMap绑定触发AbstractAction的键?

我正在构建一个InputMap和ActionMap来将键绑定到方法。许多键都可以做类似的事情。在InputMap中,每个绑定键都有一个条目。我想将几个InputMap条目与同一个ActionMap条目相关联,并在AbstractAction中使用ActionEvent参数。actionPerformed(ActionEvent事件)方法来确定按下/释放/键入的键。我查看了getID(),测试了ActionEvent是否是KeyEvent(不是)。有没有办法做到这一点,或者我必须进行不同的重构,以便每个唯一的ActionMap条目设置一个参数,然后调用我的(参数化)方法

以下是有效(但冗长)的方法:

    getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0),"myRightHandler");
    getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0),"myLeftHandler");
    getActionMap().put("myRightHandler",new AbstractAction() {
              public void actionPerformed(ActionEvent evt) {
                  System.out.println("Typed Right Arrow");
              }
          });
    getActionMap().put("myLefttHandler",new AbstractAction() {
              public void actionPerformed(ActionEvent evt) {
                  System.out.println("Typed Left Arrow");
              }
          });

以下是我想做但找不到魔力的事情:

    getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0),"myGenericHandler");
    getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0),"myGenericHandler");
    getActionMap().put("myGenericHandler",new AbstractAction() {
              public void actionPerformed(ActionEvent evt) {
                  // determine what key caused the event...
                  // evt.getKeyCode() does not work.
                  int keyCode = performMagic(evt);
                  switch (keyCode) {
                      case KeyEvent.VK_RIGHT:
                          System.out.println("Typed Right Arrow");
                          break;
                      case KeyEvent.VK_LEFT:
                          System.out.println("Typed Left Arrow");
                          break;
                      default:
                          System.out.println("Typed unknown key");
                          break;
                  }
              }
          };

共 (1) 个答案

  1. # 1 楼答案

    你应该先试试这个简单的逻辑

    getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0),"myRightHandler");
        getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0),"myLeftHandler");
        getActionMap().put("myRightHandler", new myAction("myRightHandler")); 
        getActionMap().put("myLeftHandler", new myAction("myLeftHandler")); 
    
        class myAction extends AbstractAction { 
    
            String str; 
    
            public myAction(String actName) {
    
                str = actName; 
            }
    
            public void actionPerformed(ActionEvent ae) { 
    
                switch(str) { 
    
                    case "myRightHandler": //Here is code for 'myRightHandler'. 
                    break; 
    
                    case "myLeftHandler": //Here is code for 'myLeftHandler'. 
                    break; 
                    .
                    .
                    .
                    .
                    default : //Here is default Action; 
                    break;
                }
            }
        } 
    

    现在,通过这种方式,您可以添加许多自定义组合键和操作,并使用switch将它们区分开来