我有一個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