2012-12-06 16 views
2

我有一個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!"); 
    } 
} 
+0

我們可以看到'SimpleAction'類的ActionMap,張貼一個[SSCCE](http://sscce.org) –

+1

@DavidKroukamp在那裏你去 – Fabricio

+0

爲什麼問題很難回答(無法看到擺動團隊開發者的大腦,甚至不存在,遠遠少於過去:-)如果某個Action沒有明確設置其actionCommand,則默認爲SomeThing。 SomeThing在創建actionEvent的兩個位置(在AbstractButton.fireActionEvent vs SwingUtilities.notifyAction中)不同。 – kleopatra

回答

3

除非我誤解你應該通過ae.getSource()JButton的實例調用getActionCommand這裏因爲你是在ActionEvent調用getActionCommand()

SampleAction a = new SampleAction("foo", null); 

    JButton b = new JButton(a); 
    b.getActionMap().put("bar", a); 
    b.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F1"), "bar"); 

class SampleAction extends AbstractAction 
{ 
    public SampleAction(String text, Icon icon) { 
    super(text, icon); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
    System.out.println("Action [" + ((JButton)e.getSource()).getActionCommand() + "] performed!"); 
    } 
} 

UPDATE:

感謝@Kleopatra這可能是一個小甜點呃方式:

SampleAction a = new SampleAction("foo", null); 

JButton b = new JButton(a); 
b.getActionMap().put("bar", a); 
b.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F1"), "bar"); 

class SampleAction extends AbstractAction { 

     public SampleAction(String text, Icon icon) { 
      super(text, icon); 

      putValue(Action.ACTION_COMMAND_KEY, text);//'foo' will be printed when button clicekd/F1 pressed 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.out.println("Action [" + e.getActionCommand() + "] performed!"); 
     } 
    } 
+0

嗯...這不是真的回答這個問題(正如我認爲的那樣,無論如何:-) - 當使用相同的動作作爲按鈕(在點擊時執行)時使用的_actionCommand_與使用actionMap。 – kleopatra

+0

我把'JComponent。WHEN_IN_FOCUSED_WINDOW',但當按下F1時它仍然打印出'null' – Fabricio

+0

@Fabricio請參閱更新 –

1

我有你沒有SampleAction訪問,但我猜你在構造函數中通過「foo」的文本作爲文本和具有無關的動作命令。

如果你看看AbstractButton類從JButton延伸,你看

public String getActionCommand() { 
    String ac = getModel().getActionCommand(); 
    if(ac == null) { 
     ac = getText(); 
    } 
    return ac; 
} 

產生被傳遞到操作的ActionEvent當使用這種方法。當您單擊該按鈕時,將調用此方法,並且我假設acnull,但getText()方法返回您在SampleAction類中使用的"foo"

當您通過按F1直接觸發操作時,可以繞過此機制並簡單地觸發操作。如果你想避免這種情況,你可以添加一個ActionJButton簡單地做JButton#doClick,這是API呼叫來執行「點擊」按鈕

+0

哦,所以這並不重要。我只是認爲我在鍵盤綁定方面做了一些錯誤。 :) – Fabricio

+1

「然而...」有點誤導:沒有任何旁路,只是在不同位置創建的ActionEvent,請參閱SwingUtilities.notifyAction :)默認值只是...默認值,並且不可控制(甚至在這裏甚至是無證的)。如果我們需要控制它們,唯一的出路是用它配置動作。 – kleopatra

相關問題