2016-09-16 72 views
0

下面是我現在正在使用的,當我在GUI中測試它時,項目價格剛好從0.00更改爲0.0。到目前爲止,我已經嘗試了至少3種不同的方法來創建對象,並使用calcPrice()方法調用它們,但目前爲止沒有任何成功。我所做的唯一一步就是現在GUI保持打開而不會崩潰。使用NetBeanse上的GUI生成器接口將事件處理函數調用到事件處理函數

〜讓我知道你是否需要更多我的代碼來評估這個。我只是複製了GUI

private void DoubleBurgerActionPerformed(java.awt.event.ActionEvent evt) {            
    ItemPrice.setText(String.valueOf(price)); 
}            
      public double calcPrice(boolean singleBurg, boolean doubleBurg, boolean cheese, boolean bacon, boolean meal) 
    { 

     price=0; 
     if (singleBurg) 
     { 
      price+=3.50; 

     } 
     if (doubleBurg) 
     { 
      price+=4.50; 
     } 
     if (cheese) 
     { 
      price+=.50; 
     } 
     if (bacon) 
     { 
      price+=1.25; 
     } 
     if (meal) 
     { 
      price+=4.00; 
     } 
     return price; 
    } 

回答

0

的將String.valueOf()將轉換成0.00至0.0的這一部分,你可以使用的String.format小數點後2位數字的輸出格式。

private void DoubleBurgerActionPerformed(java.awt.event.ActionEvent evt) {            
    // ItemPrice.setText(String.valueOf(price)); 
    ItemPrice.setText(String.format("%.2f", price)); 
}            
相關問題