JavaFX - 有没有办法循环复选框?
JavaFX - Is there a way to loop a checkbox?
我想知道是否有一种循环复选框而不是使用一堆 IF 循环的方法。
我关心的是:有没有办法循环这个以及为什么我不能在调节复选框时使用 Else If。
这是我的代码:
@FXML
private CheckBox cb1;
@FXML
private CheckBox cb2;
@FXML
private CheckBox cb3;
@FXML
private CheckBox cb4;
public void checkEvent(ActionEvent event) {
int price = 0;
String message = "";
if (cb1.isSelected()) {
price = 150;
message += cb1.getText() + " Price: " + price + "\n";
}
if (cb2.isSelected()) {
price = 200;
message += cb2.getText() + " Price: " + price + "\n";
}
if (cb3.isSelected()) {
price = 100;
message += cb3.getText() + " Price: " + price + "\n";
}
if (cb4.isSelected()) {
price = 200;
message += cb4.getText() + " Price: " + price +"\n";
}
lbllist.setText(message);
}
输出:
假设我有
- 薯条价格150
- 汉堡价格 200
- 沙拉价格 100
- 培根价格200
所以我总共有650块钱
输出有效,但如果我将这些数据放入模型 class,它遵循 MVC 模式。它不会累加或增加值。
所以看起来 checkEvent
函数是针对单个复选框的,然后也发展为处理其他复选框。所以你看到你的检查事件函数是如何重复很多相同的代码的吗
if (cb1.isSelected()) {
price = 150;
message += cb1.getText() + " Price: " + price + "\n";
}
例如,如果您可以提取正在更改的内容并将其作为参数传递,那么您只需要一两个 if 语句而不是 4 或您传递它的任意数量的复选框。 private void checkEvent(CheckBox checkBox, int price)
我拉出价格是因为它有能力不同,我通过了复选框,这样它就可以自己到达设置的文本然后你只需要写一次代码
private void checkEvent(CheckBox checkBox, int price){
String message = checkBox.getText() + " Price: " + price + "\n";
if(checkBox.isSelected()){
totalCost = totalCost + price;
messageListLabel.setText(messageListLabel.getText() + message);
} else {
totalCost = totalCost - price;
messageListLabel.setText(messageListLabel.getText().replace(message, ""));
}
totalCostLabel.setText("\nTotal Cost:"+totalCost);
}
下面是一个完整的可运行示例
public class Main extends Application {
private int totalCost = 0;
private Label messageListLabel;
private Label totalCostLabel;
@Override
public void start(Stage primaryStage) {
CheckBox checkBoxOne = new CheckBox("Fries");
checkBoxOne.setOnAction(event -> checkEvent(checkBoxOne, 150));
CheckBox checkBoxTwo = new CheckBox("Burger");
checkBoxTwo.setOnAction(event -> checkEvent(checkBoxTwo, 200));
CheckBox checkBoxThree = new CheckBox("Salmon");
checkBoxThree.setOnAction(event -> checkEvent(checkBoxThree, 100));
CheckBox checkBoxFour = new CheckBox("Bacon");
checkBoxFour.setOnAction(event -> checkEvent(checkBoxFour, 200));
messageListLabel = new Label();
totalCostLabel = new Label();
VBox vbox = new VBox(
new Label("Pic what ya want!!!"),
checkBoxOne,
checkBoxTwo,
checkBoxThree,
checkBoxFour,
messageListLabel,
totalCostLabel
);
vbox.setAlignment(Pos.CENTER);
primaryStage.setScene(new Scene(vbox, 150, 250));
primaryStage.show();
}
private void checkEvent(CheckBox checkBox, int price){
String message = checkBox.getText() + " Price: " + price + "\n";
if(checkBox.isSelected()){
totalCost = totalCost + price;
messageListLabel.setText(messageListLabel.getText() + message);
} else {
totalCost = totalCost - price;
messageListLabel.setText(messageListLabel.getText().replace(message, ""));
}
totalCostLabel.setText("\nTotal Cost:"+totalCost);
}
}
我想知道是否有一种循环复选框而不是使用一堆 IF 循环的方法。
我关心的是:有没有办法循环这个以及为什么我不能在调节复选框时使用 Else If。
这是我的代码:
@FXML
private CheckBox cb1;
@FXML
private CheckBox cb2;
@FXML
private CheckBox cb3;
@FXML
private CheckBox cb4;
public void checkEvent(ActionEvent event) {
int price = 0;
String message = "";
if (cb1.isSelected()) {
price = 150;
message += cb1.getText() + " Price: " + price + "\n";
}
if (cb2.isSelected()) {
price = 200;
message += cb2.getText() + " Price: " + price + "\n";
}
if (cb3.isSelected()) {
price = 100;
message += cb3.getText() + " Price: " + price + "\n";
}
if (cb4.isSelected()) {
price = 200;
message += cb4.getText() + " Price: " + price +"\n";
}
lbllist.setText(message);
}
输出:
假设我有
- 薯条价格150
- 汉堡价格 200
- 沙拉价格 100
- 培根价格200
所以我总共有650块钱
输出有效,但如果我将这些数据放入模型 class,它遵循 MVC 模式。它不会累加或增加值。
所以看起来 checkEvent
函数是针对单个复选框的,然后也发展为处理其他复选框。所以你看到你的检查事件函数是如何重复很多相同的代码的吗
if (cb1.isSelected()) {
price = 150;
message += cb1.getText() + " Price: " + price + "\n";
}
例如,如果您可以提取正在更改的内容并将其作为参数传递,那么您只需要一两个 if 语句而不是 4 或您传递它的任意数量的复选框。 private void checkEvent(CheckBox checkBox, int price)
我拉出价格是因为它有能力不同,我通过了复选框,这样它就可以自己到达设置的文本然后你只需要写一次代码
private void checkEvent(CheckBox checkBox, int price){
String message = checkBox.getText() + " Price: " + price + "\n";
if(checkBox.isSelected()){
totalCost = totalCost + price;
messageListLabel.setText(messageListLabel.getText() + message);
} else {
totalCost = totalCost - price;
messageListLabel.setText(messageListLabel.getText().replace(message, ""));
}
totalCostLabel.setText("\nTotal Cost:"+totalCost);
}
下面是一个完整的可运行示例
public class Main extends Application {
private int totalCost = 0;
private Label messageListLabel;
private Label totalCostLabel;
@Override
public void start(Stage primaryStage) {
CheckBox checkBoxOne = new CheckBox("Fries");
checkBoxOne.setOnAction(event -> checkEvent(checkBoxOne, 150));
CheckBox checkBoxTwo = new CheckBox("Burger");
checkBoxTwo.setOnAction(event -> checkEvent(checkBoxTwo, 200));
CheckBox checkBoxThree = new CheckBox("Salmon");
checkBoxThree.setOnAction(event -> checkEvent(checkBoxThree, 100));
CheckBox checkBoxFour = new CheckBox("Bacon");
checkBoxFour.setOnAction(event -> checkEvent(checkBoxFour, 200));
messageListLabel = new Label();
totalCostLabel = new Label();
VBox vbox = new VBox(
new Label("Pic what ya want!!!"),
checkBoxOne,
checkBoxTwo,
checkBoxThree,
checkBoxFour,
messageListLabel,
totalCostLabel
);
vbox.setAlignment(Pos.CENTER);
primaryStage.setScene(new Scene(vbox, 150, 250));
primaryStage.show();
}
private void checkEvent(CheckBox checkBox, int price){
String message = checkBox.getText() + " Price: " + price + "\n";
if(checkBox.isSelected()){
totalCost = totalCost + price;
messageListLabel.setText(messageListLabel.getText() + message);
} else {
totalCost = totalCost - price;
messageListLabel.setText(messageListLabel.getText().replace(message, ""));
}
totalCostLabel.setText("\nTotal Cost:"+totalCost);
}
}