【发布时间】: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