每次都不创建新实例的键绑定

时间:2014-11-07 19:10:27

标签: java swing key-bindings

结帐这篇文章的第一个答案:Java Keybindings 特别是,这些行:

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

他总是在创造他班级的新对象,是否有可能没有它?检查下面的代码,我尝试了ActionEvent变量“e”的所有方法,但所有这些方法都是无用的。每次创建新对象都很好吗? 到目前为止我有这个:

action = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
        // can I determine here what action was performed?
        }
    };

    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left_arrow");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right_arrow");
    actionMap.put("left_arrow", action);
actionMap.put("right_arrow", action);

0 个答案:

没有答案