2016-11-09 72 views
0

我正在使用javaFX和Scenebuilder製作Car Care Center GUI,並且我一直在努力,直到遇到一個問題。根據ChoiceBox的選擇添加價格

問題確實很簡單。基本上,我做了這個選擇框,用戶可以選擇替換常規輪胎(225美元)或運動輪胎(310美元)。 而且我必須將費用加在總和上(變量名稱= costSum)。 如果用戶首先在choicebox中選擇普通輪胎,costSum僅增加225美元。

然後,如果用戶選擇運動輪胎,它應該只增加310美元(普通輪胎不是225美元),但costSum增加225和310,因爲在選擇運動輪胎之前,225美元被添加到costSum

我知道這是爲什麼會發生,但我無法找到解決這個問題的方法,即使認爲它看起來很簡單。

在用戶從選擇框中選擇選項之前,有沒有辦法將costSum的值重置爲原始值?

爲了簡化我的問題,當用戶選擇每個不同的選項(普通輪胎,運動輪胎)時,如何僅向costSum添加一個成本值?

下面是該選擇框代碼:

@FXML 
void onSelectTireReplacementChoice(ActionEvent event) { 

    if(tireReplacementChkBox.isSelected()){ 
     tireReplacementChoiceBox.setDisable(false);  
     tireReplacementChoiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() { 

      public void changed(ObservableValue ov, Number value, Number selection) { 

       if(selection.intValue() == 0){ 
        costSum += REGULAR_TIRE;     
       } 

       if(selection.intValue() == 1){ 
        costSum += SPORTS_TIRE;      
       } 
     ; 
       String cost = "Service Cost: " + "$" + df.format(costSum); 
       serviceCostLabel.setText(cost); 
      } 
     }); 
    } else{ 
     // costSum += initialCost; 
     String cost = "Service Cost: " + "$" + df.format(costSum); 
     serviceCostLabel.setText(cost); 
     tireReplacementChoiceBox.setDisable(true);  
    }  

} 

這是整個代碼(我沒有完成尚未雖然):

public class FXMLDocumentController implements Initializable { 
    public double costSum = 0; 
    public double costOnCustomerType; 
    public boolean update; 
    public int choice; 
    public double sum = 0; 

    String cost = "Service Cost: " + "$"; 

    final double BRAKES = 27.27; 
    final double FLUID_CHK = 9.09; 
    final double CAR_WASH = 4.54; 
    final double EMMISION_INSPECTION = 36.37; 
    final double TIRE_ROTATION = 18.18; 
    final double REGULAR_TIRE = 225.22; 
    final double SPORTS_TIRE = 315.32; 
    final double REGULAR_OIL = 13.64; 
    final double SYNTHETIC_OIL = 24.54; 

    public double initialCost ; 

    DecimalFormat df = new DecimalFormat("#.00"); 

    @FXML 
    private Label label; 

    @FXML 
    private RadioButton newCustomerRadioButton; 

    @FXML 
    private ToggleGroup customerType; 

    @FXML 
    private RadioButton regularCustomerRadioButton; 

    @FXML 
    private TextField nameTextField; 

    @FXML 
    private TextField phoneTextField; 

    @FXML 
    private TextField addressTextField; 

    @FXML 
    private TextField emailTextField; 

    @FXML 
    private Button printInvoiceButtion; 

    @FXML 
    private Label serviceCostLabel; 

    @FXML 
    private Button resetButton; 

    @FXML 
    private CheckBox brakesChkBox; 

    @FXML 
    private CheckBox tireRotationChkBox; 

    @FXML 
    private CheckBox fluidChkBox; 

    @FXML 
    private CheckBox carWashChkBox; 

    @FXML 
    private CheckBox inspectionChkBox; 

    @FXML 
    private CheckBox tireReplacementChkBox; 

    @FXML 
    private CheckBox oilChangeChkBox; 

    @FXML 
    private ChoiceBox tireReplacementChoiceBox; 

    @FXML 
    void updateBrakes(ActionEvent event) { 

     if(brakesChkBox.isSelected()){ 
      costSum += BRAKES; 
     }else{ 
      costSum -= BRAKES; 
     } 

     String cost = "Service Cost: " + "$" + df.format(costSum); 
     serviceCostLabel.setText(cost); 
    } 

    @FXML 
    void updateCarWash(ActionEvent event) { 

    } 

    @FXML 
    void updateEmmissonInspec(ActionEvent event) { 

    } 

    @FXML 
    void updateFluidCheck(ActionEvent event) { 

    } 

    @FXML 
    void updateTireRotation(ActionEvent event) { 
     if(tireRotationChkBox.isSelected()){ 
      costSum += TIRE_ROTATION; 
     } else{ 
      costSum -= TIRE_ROTATION;  
     } 

     serviceCostLabel.setText(cost + df.format(costSum)); 


    } 


    @FXML 
    void onChangeServiceCostRequest(ActionEvent event) { 

    } 

    @FXML 
    void onSelectOilChange(ActionEvent event) { 

    } 

    @FXML 
    void onSelectTireReplacementChoice(ActionEvent event) { 

     if(tireReplacementChkBox.isSelected()){ 
     tireReplacementChoiceBox.setDisable(false);  
     tireReplacementChoiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() { 

       public void changed(ObservableValue ov, Number value, Number selection) { 

        if(selection.intValue() == 0){ 
         costSum += REGULAR_TIRE;     
        } 

        if(selection.intValue() == 1){ 
         costSum += SPORTS_TIRE;      
        } 
      ; 
        String cost = "Service Cost: " + "$" + df.format(costSum); 
        serviceCostLabel.setText(cost); 
       } 
      }); 
     } else{ 
      // costSum += initialCost; 
       String cost = "Service Cost: " + "$" + df.format(costSum); 
       serviceCostLabel.setText(cost); 
     tireReplacementChoiceBox.setDisable(true);  
     }  

    } 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     tireReplacementChoiceBox.setItems(FXCollections.observableArrayList("Regular Tire", "Sports TIre")); 
     tireReplacementChoiceBox.setDisable(true); 


     String cost = "Service Cost: " + "$" + df.format(costSum); 
     serviceCostLabel.setText(cost);// TODO 

    }  

} 
+0

待辦事項不按照你這樣做的方式更新價值。您正在更新更新。您應該更新不同操作的值。就像按下按鈕時一樣。你可以使用onchange來設置一個變量,這樣當一個按鈕被按下時,正確的計算就完成了。在這種情況下,我個人根本不會使用onchange方法。我會在提交按鈕後使用comboBox.getSelectionModel()。getSelectedItem()來獲取值。如果你的gui上有一個你想要更新的標籤,那麼在這種情況下,我會使用更改。 – Sedrick

+0

謝謝你的幫助!有沒有按鈕來改變價值我的老師給我們示例,並有標籤,顯示總成本,它每次更改用戶檢查複選框或ChoiceBox!然後我會尋找使用onchange方法! – yds725

回答

0

試試這個:

@FXML 
void onSelectTireReplacementChoice(ActionEvent event) { 
    double newValue = 0; 

    if(tireReplacementChkBox.isSelected()){ 
     tireReplacementChoiceBox.setDisable(false);    

     tireReplacementChoiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() { 

      public void changed(ObservableValue ov, Number value, Number selection) { 

       if(selection.intValue() == 0){ 
        newValue = REGULAR_TIRE;     
       } 

       if(selection.intValue() == 1){ 
        newValue = SPORTS_TIRE;      
       } 
     ; 
       String cost = "Service Cost: " + "$" + df.format(newValue); 
       serviceCostLabel.setText(cost); 
      } 
     }); 
    } else{ 
     costSum = initialCost + newValue; 
     String cost = "Service Cost: " + "$" + df.format(costSum); 
     serviceCostLabel.setText(cost); 
     tireReplacementChoiceBox.setDisable(true);  
    }  

}