【问题标题】:Adding price based on ChoiceBox selection根据 ChoiceBox 选择添加价格
【发布时间】:2016-11-09 07:10:48
【问题描述】:

我正在使用 javaFX 和 Scenebuilder 制作 Car Care Center GUI,在遇到一个问题之前我做得很好。

问题其实很简单。基本上,我做了一个选择框,用户可以选择更换普通轮胎(225 美元)或运动轮胎(310 美元)。 我必须将成本加到总和中(变量名 = costSum)。 如果用户先在选择框中选择普通轮胎,costSum 仅增加 225 美元。

然后如果用户选择运动轮胎,它应该只添加 310 美元(不是普通轮胎的 225 美元),但costSum 添加了 225 和 310,因为在选择运动轮胎之前,costSum 添加了 225 美元。

我知道为什么会发生这种情况,但即使看起来很简单,我也找不到解决方法。

在用户从 choicbox 中选择选项之前,有没有办法将 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

    }    

}

【问题讨论】:

  • 不要按照你的方式更新值。您正在更新更改。您应该更新不同操作的值。就像按下按钮一样。您可以使用 onchange 设置变量,以便在按下按钮时完成正确的计算。在这种情况下,我个人根本不会使用 onchange 方法。在提交按钮后,我将使用 comboBox.getSelectionModel().getSelectedItem() 获取值。如果您的 gui 上有一个标签要在更改时更新,在这种情况下我会在更改时使用。
  • 感谢您的帮助!没有按钮可以更改值 我的老师向我们展示了示例,并且有显示总成本的标签,并且每次用户选中复选框或选择框时它都会更改!那我就随便找用onchange方法了!

标签: java javafx fxml scenebuilder


【解决方案1】:

试试这个:

@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);     
    }       

}

【讨论】:

    猜你喜欢
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    相关资源
    最近更新 更多