2012-11-11 50 views
0

我想要點擊一個GUI按鈕來顯示小數點。我使用下面的方法,它適用於數字而不是小數點。任何建議將是偉大的。感謝如何從虛擬數字鍵盤獲取小數點?

private void displayWeightedquantity(ActionEvent event){ 
    JButton currentButton = null; 
    JButton[] numberButtonsarray = {bnDecimal,bnZero,bnOne, bnTwo, bnThree, bnFour, bnFive,bnSix,bnSeven, 
            bnEight,bnNine}; 

    currentButton = (JButton)event.getSource(); 
    for (int i = 0; i <numberButtonsarray.length; i++){ 
     if (currentButton == numberButtonsarray[i]){ 
      lbDisplayitemQty.setText(lbDisplayitemQty.getText() + currentButton.getText()); 
      break; 
      } 
     } 
    } 

我的虛擬數字小:

enter image description here

+2

爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 –

+0

順便說一句 - 我懷疑'ActionEvent.getActionCommand()'會產生大多數代碼被設計來確定的字符。 –

+3

你爲什麼要循環按鈕?相反,你可以有; '如果(!currentButton = bnPurcahse || currentButton = bnClear) lbDisplayitemQty.setText(lbDisplayitemQty.getText()+ currentButton.getText());' 這是實現相同的功能的一個更有效的方式。 不管怎樣,你的代碼對我來說看起來不錯,所以我會假設你有一些錯誤的類型錯誤,使用'bnDecimal'導致''currentButton == numberButtonsarray [i]'永遠不會被'.'滿足。我沒有在該邏輯中看到任何錯誤,所以我會看看其他地方(或採取上述建議:)。 – evanmcdonnal

回答

1

我懷疑問題是別的地方在你的代碼。

我設置的測試作品找到了。

您需要提供一個功能示例,顯示問題,而不僅僅是代碼段。

同時,確保十進制按鈕都有它的動作監聽連接