有没有办法在关键事件后返回一个值?

时间:2016-08-18 01:51:29

标签: java

我有这个关键的活页夹:

public static void main(String[] args){
  //does stuff
    InputMap im = panel.getInputMap(panel.WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = panel.getActionMap();

    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "RightArrow");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "LeftArrow");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "UpArrow");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "DownArrow");

    am.put("RightArrow", new ArrowAction("RightArrow"));
    am.put("LeftArrow", new ArrowAction("LeftArrow"));
    am.put("UpArrow", new ArrowAction("UpArrow"));
    am.put("DownArrow", new ArrowAction("DownArrow"));
   }

  class ArrowAction extends AbstractAction {

        private String cmd;

        public ArrowAction(String cmd) {
            this.cmd = cmd;
        }

        public void actionPerformed(ActionEvent e) {
            if (cmd.equalsIgnoreCase("LeftArrow")) {
                System.out.println("The left arrow was pressed!");
            } else if (cmd.equalsIgnoreCase("RightArrow")) {
                System.out.println("The right arrow was pressed!");
            } else if (cmd.equalsIgnoreCase("UpArrow")) {
                System.out.println("The up arrow was pressed!");
            } else if (cmd.equalsIgnoreCase("DownArrow")) {
                System.out.println("The down arrow was pressed!");
            }
        }
      }

我希望我的程序知道何时按下箭头键(它已经执行),然后将值返回到main,然后再次等待另一个箭头按下。有可能吗?

0 个答案:

没有答案