2016-03-09 52 views
0

我的項目中有兩個按鈕,都帶有「+」標籤。當調用actionPerformed()方法時,它會根據標籤調用特定的方法。我怎樣才能在兩個具有相同標籤的JButton之間進行區分?有沒有更好的方式來做到這一點,然後我是如何做到的?在Java中使用多個具有相同標籤的JButtons

這是按鈕的定義:

JButton keypadPlus1 = new JButton(" + "); 
JButton keypadMinus1 = new JButton(" - "); 
JButton keypadPlus2 = new JButton("+"); 
JButton keypadMinus2 = new JButton("-"); 

添加的ActionListeners的按鈕:

keypadPlus1.addActionListener(backEnd); 
keypadPlus2.addActionListener(backEnd); 
keypadMinus1.addActionListener(backEnd); 
keypadMinus2.addActionListener(backEnd); 

的actionPerformed @覆蓋在後端:

public void actionPerformed (ActionEvent event) { 
     String command = event.getActionCommand(); 
     if (command.equals("+")) { 
      calcLifePoints(command); 
     } 
     if (command.equals("-")) { 
      calcLifePoints(command); 
     } 
     if (command.equals(" + ")) { 
      calcLifePoints(command); 
     } 
     if (command.equals(" - ")) { 
      calcLifePoints(command); 
     } 

    } 
+0

歡迎計算器!您能否詳細說明您的問題,比如代碼或其他事情,以便人們能夠儘早解決問題並幫助您?謝謝! – manetsus

+0

'ActionEvent.getSource()'雖然它可能應該有單獨的偵聽器。 –

+0

使用單獨的偵聽器。不要混淆你的責任 –

回答

0

取而代之的是,

public void actionPerformed (ActionEvent event) { 
     String command = event.getActionCommand(); 
     if (command.equals("+")) { 
      calcLifePoints(command); 
     } 
     if (command.equals("-")) { 
      calcLifePoints(command); 
     } 
     if (command.equals(" + ")) { 
      calcLifePoints(command); 
     } 
     if (command.equals(" - ")) { 
      calcLifePoints(command); 
     } 

    } 

使用這樣,

public void actionPerformed (ActionEvent event) { 
     Object command = event.getSource(); 
     if (command.equals(keypadPlus1)) { 
      calcLifePoints(event.getActionCommand()); 
     } 
     if (command.equals(keypadMinus1)) { 
      calcLifePoints(event.getActionCommand()); 
     } 
     if (command.equals(keypadPlus2)) { 
      calcLifePoints(event.getActionCommand()); 
     } 
     if (command.equals(keypadMinus2)) { 
      calcLifePoints(event.getActionCommand()); 
     } 

    } 
2

你可以...

使用ActionEvent#getSource

你可以...

設置每個按鈕的actionCommand屬性獨特的東西,並使用ActionEvent#getActionCommand

你可以...

使用單獨的聽衆,無論是匿名或根據您的需要內部或外部類

您可以...

利用Action API,這將允許你定義,明確了公共屬性(如+文本),然後擴展本作獨特的行動爲每個按鈕

更多見How to Use Actions共同/抽象Action的詳情

你可以...

使用JButton#putClientProperty設置在每個按鈕上一些獨特的標誌,並鑄ActionEventJComponent和使用getClientProperty檢索標誌...但考慮到以前的建議,我不知道爲什麼你會打擾

2

您不應該讓單個偵聽器處理不同責任的行爲。如果兩個+按鈕沒有做同樣的事情,給按鈕單獨的偵聽器。

這將使您的代碼更加緊密。通過將您的聽衆分別減少爲1個責任,您將能夠重新使用這些責任。它還使測試更容易,使您可以完全隔離地測試每個行爲。

雖然如果你必須,ActionEvent#getSource()返回哪個組件觸發事件。做一個參考比較將允許您確定哪個對象觸發了事件。


的最佳方式來處理這將爲當前的聽衆有責任分成單獨的類:

class FirstListener implements ActionListener { 
    @Override 
    public void actionPerformed(ActionEvent e) { 

    } 
} 

讓我們假設FirstListener代表你的第一+按鈕的行爲。如果該行爲需要任何外部物體(例如在不同的類的字段),簡單地通過構造傳遞:

class FirstListener implements ActionListener { 
    private Object dependency; 

    public FirstListener(Object dependency) { 
     this.dependency = dependency; 
    } 

    //actionPerformed declaration 
} 

可以爲其它按鈕做相同的(例如,第二+按鈕) 。

如果你覺得這是有點過分,隨意使用lambda表達式來聲明聽衆:

//Java 8+ 
button.addActionListener(event -> { 

}); 

這不會給你相同的模塊化與前面的例子,作爲行爲是不與實際類更長的距離:您將被迫更改實現以更改行爲,而不是使用依賴關係反轉來簡單傳遞也實現ActionListener的其他對象。

+0

我將如何去使用兩個不同的聽衆? – VanSpoof

+0

@VanSpoof只需創建一個新的監聽器。 'ActionListener'是一個*功能接口*,這意味着你可以通過lambda表達式來定義它。雖然如果你想保持模塊化,你可以爲每個監聽器(它實現'ActionListener')創建一個新類,傳遞構造函數需要的任何依賴關係。 –

+0

非常感謝您的幫助,我非常感謝。 :) – VanSpoof

相關問題