Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX-有没有一种循环复选框的方法?_Java_Javafx - Fatal编程技术网

JavaFX-有没有一种循环复选框的方法?

JavaFX-有没有一种循环复选框的方法?,java,javafx,Java,Javafx,我想知道是否有一种方法可以循环一个复选框,而不是使用一堆if循环 我在这里关心的是:是否有一种方法可以循环此操作,以及为什么我不能使用Else Ifwhen conditioning复选框 这是我的密码: @FXML private CheckBox cb1; @FXML private CheckBox cb2; @FXML private CheckBox cb3; @FXML private CheckBox cb4; public void checkEvent(ActionE

我想知道是否有一种方法可以循环一个复选框,而不是使用一堆if循环

我在这里关心的是:是否有一种方法可以循环此操作,以及为什么我不能使用Else Ifwhen conditioning复选框

这是我的密码:

@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块钱


输出是有效的,但是如果我将这些数据放在一个模型类中,它遵循MVC模式。它不会累加或增加值。

因此它看起来像是
checkEvent
函数用于单个复选框,然后扩展为处理其他复选框。那么,您是否看到在check事件函数中有许多相同的代码被重复

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

请..@kleopatra查看更新的问题。非常感谢您对e的投入。Gcb2使用<代码>价格+=200而不是
价格=200你好,先生,只是一个简单的问题。。我不需要为这个循环创建一个循环或每个循环?这是否适用于Main.java和MainController.java类结构?不是把所有的东西都放进一个Main?你好,我不能在MainController类中运行它。解析onAction='#checkEvent'时出错,原因可能是事件处理程序不在命名空间中,或者脚本中有错误。我无法判断您是否已解决,因为您已将答案标记为已解决。您不需要创建循环,只需在每个复选框上设置循环即可。您可以自己在一个单独的类中运行它,而不需要另一个类来启动它。对于您得到的最后一个错误,您将无法从fxml设置on action事件,您可能希望在控制器类的构造函数中执行此操作,并且要在构造函数中设置它们,您将执行类似以下操作
checkBoxOne.setOnAction(event->checkEvent(checkBoxOne,150))针对您的每个复选框