2016-11-09 31 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);  
    }  

}