【问题标题】:GUI keeps compounding discount when I toggle JCheckBox当我切换 JCheckBox 时,GUI 不断增加折扣
【发布时间】:2016-11-15 04:19:28
【问题描述】:

我正在尝试制作一个 GUI,当您单击两个单选按钮之一时,它将根据您是新客户还是老客户应用不同的折扣。折扣将应用于您可以选择的每个不同选项。我的问题是,每次我单击复选框添加选项时,它都会不断增加折扣。第一次是对的,但之后的每一次点击都让折扣越来越高。这是我的复选框代码:

@FXML
    void tires(ActionEvent event) {
        if(isNewCustomer==true){
            tireRotation=tireRotation*(1.0-newCustomerDiscount);
            //costLabel.setText("$ " +DF.format(cost));
        }else{
            tireRotation=tireRotation*(1.0-regularCustomerDiscount);
           // costLabel.setText("$ " +DF.format(cost));
        }

        if(tireBox.isSelected()){
            cost+=tireRotation;
            costLabel.setText("$ " +DF.format(cost));

        } else {
            cost =cost-tireRotation;
            costLabel.setText("$ " +DF.format(cost));
        }

    }

这是我的单选按钮代码:

@FXML
void newCustomer(ActionEvent event) {
    if (newCustomer.isSelected()){
        isNewCustomer=true;
    }
}

任何帮助将不胜感激!

【问题讨论】:

  • 首先,您可以删除isNewCustomer ==true,只使用isNewCustomer,因为它是boolean。其次,如果没有选择新客户,它应该设置isNewCustomer = false,我没有看到。第三,您要覆盖tireRotation,这是我假设的基本金额。我认为您应该使用一个新变量tireRotationCost = tireRotation*(1.0-new/regularCustomerDiscount);。这样,它就不会复合,而是每次都覆盖成本。
  • 也许可以用一种方法来处理两个切换。

标签: user-interface javafx scenebuilder jcheckbox jradiobutton


【解决方案1】:

我刚刚重读了你的问题。创建全局变量。在您的切换框方法中设置 finalCost 全局变量。 在 if 中设置变量示例:

全局变量:

double finalCost = 0;
double initialCharge = 200;
double discount= 50;
double newAccountDiscount= 25;

第一种方法:

if(tireBox.isSelected() && newCustomer.isSelected())
{
    finalCost = initialCharge - discount - newAccountDiscount;
}
else if(tireBox.isSelected())
{
   finalCost = initialCharge - discount;
}
else if(newCustomer.isSelected())
{
   finalCost = initialCharge - newAccountDiscount;
}
else
{
   finalCost = initalCharge;
} 

第二种方法:

if(tireBox.isSelected() && newCustomer.isSelected())
{
    finalCost = initialCharge - discount - newAccountDiscount;
}
else if(tireBox.isSelected())
{
   finalCost = initialCharge - discount;
}
else if(newCustomer.isSelected())
{
   finalCost = initialCharge - newAccountDiscount;
}
else
{
   finalCost = initalCharge;
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 2020-10-24
    相关资源
    最近更新 更多