我有一个Action
SampleAction a = new SampleAction("foo", null);
然后我将它添加到 Button 和 ActionMap
JButton b = new JButton(a);
b.getActionMap().put("bar", a);
b.getInputMap().put(KeyStroke.getKeyStroke("F1"), "bar");
System.out.println("Action [" + e.getActionCommand() + "] performed!");
我在 Action 中添加了一个跟踪 ( )。当我用鼠标按下按钮时,它显示
Action [foo] performed!
但是当我使用时F1,它显示:
Action [null] performed!
为什么?
class SampleAction extends AbstractAction
{
public SampleAction(String text, Icon icon) {
super(text, icon);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Action [" + e.getActionCommand() + "] performed!");
}
}