我有一个Action
行动和ActionMap中 - 解释我这种行为
SampleAction a = new SampleAction("foo", null);
然后我把它添加到一个按钮,并以关系的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 [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!");
}
}
我们可以看到'SimpleAction'类的
ActionMap
,张贴一个[SSCCE](http://sscce.org) –@DavidKroukamp在那里你去 – Fabricio
为什么问题很难回答(无法看到摆动团队开发者的大脑,甚至不存在,远远少于过去:-)如果某个Action没有明确设置其actionCommand,则默认为SomeThing。 SomeThing在创建actionEvent的两个位置(在AbstractButton.fireActionEvent vs SwingUtilities.notifyAction中)不同。 – kleopatra