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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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_User Interface_Javafx_Checkbox - Fatal编程技术网

JAVAFX更改后如何更新复选框取消选择?

JAVAFX更改后如何更新复选框取消选择?,java,user-interface,javafx,checkbox,Java,User Interface,Javafx,Checkbox,您好,我正在尝试编写一个JavaFXGUI应用程序,允许用户使用一组复选框选择一组比萨配料。假设每种配料的价格为50美分,一个普通比萨饼的价格为10美元,则显示比萨饼的价格。请注意,一旦勾选或取消勾选了配料,比萨饼的价格应该会自动更新。我几乎让它工作,但当我点击多个框的价格变化,我试图+0.50美分每浇头,如果我取消选择复选框-0,50美分,但当我选择多个复选框,它是添加更多。我怎么能换这个谢谢 import javafx.scene.control.CheckBox; import javaf

您好,我正在尝试编写一个JavaFXGUI应用程序,允许用户使用一组复选框选择一组比萨配料。假设每种配料的价格为50美分,一个普通比萨饼的价格为10美元,则显示比萨饼的价格。请注意,一旦勾选或取消勾选了配料,比萨饼的价格应该会自动更新。我几乎让它工作,但当我点击多个框的价格变化,我试图+0.50美分每浇头,如果我取消选择复选框-0,50美分,但当我选择多个复选框,它是添加更多。我怎么能换这个谢谢

import javafx.scene.control.CheckBox;
import javafx.scene.text.Text;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;


public class PizzaMenu extends VBox
{
    private double cost;
    private Text text;
    private CheckBox checkbox1, checkbox2, checkbox3, checkbox4, checkbox5, checkbox6;
    public PizzaMenu()
    {
        cost=10.00;
        text=new Text("Pizza Cost:" + cost);
        checkbox1 = new CheckBox("Extra Cheese");
        checkbox1.setOnAction(this::processCheckBox);
        checkbox2 = new CheckBox("Green Pepper");
        checkbox2.setOnAction(this::processCheckBox);
        checkbox3 = new CheckBox("Pepperoni");
        checkbox3.setOnAction(this::processCheckBox);
        checkbox4 = new CheckBox("Onion");
        checkbox4.setOnAction(this::processCheckBox);
        checkbox5 = new CheckBox("Sausage");
        checkbox5.setOnAction(this::processCheckBox);
        checkbox6 = new CheckBox("Anchovies");
        checkbox6.setOnAction(this::processCheckBox);
        //layout container 4 hbox, 2 vbox?
        HBox hSet1 = new HBox(checkbox1, checkbox2);
        hSet1.setAlignment(Pos.CENTER);
        HBox hSet2 = new HBox(checkbox3, checkbox4);
        hSet2.setAlignment(Pos.CENTER);
        HBox hSet3 = new HBox(checkbox5, checkbox6);
        hSet3.setAlignment(Pos.CENTER);
        HBox userCost = new HBox(text);
        userCost.setAlignment(Pos.CENTER);
        VBox vSet1 = new VBox(hSet1,hSet2,hSet3,userCost);
        vSet1.setAlignment(Pos.CENTER);
        setSpacing(20);
        getChildren().addAll(vSet1);
    }
    public void processCheckBox(ActionEvent event)
    {
        if(checkbox1.isSelected() || checkbox2.isSelected() || checkbox3.isSelected() || checkbox4.isSelected() || checkbox5.isSelected() || checkbox6.isSelected())
        {
            cost = cost + 0.50;
            text.setText("Pizza Cost:" + cost);
        }
        else if(!checkbox1.isSelected() || !checkbox2.isSelected() || !checkbox3.isSelected() || !checkbox4.isSelected() || !checkbox5.isSelected() || !checkbox6.isSelected())
        {
            cost = cost - 0.50;
            text.setText("Pizza Cost:" + cost);
        }
    }
}
//更新

import javafx.scene.control.CheckBox;
import javafx.scene.text.Text;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;


public class PizzaMenu extends VBox
{
    private double cost;
    private Text text;
    private CheckBox checkbox1, checkbox2, checkbox3, checkbox4, checkbox5, checkbox6;
    public PizzaMenu()
    {
        cost=10.00;
        text=new Text("Pizza Cost:" + cost);
        checkbox1 = new CheckBox("Extra Cheese");
        checkbox1.setOnAction(this::processCheckBox);
        checkbox2 = new CheckBox("Green Pepper");
        checkbox2.setOnAction(this::processCheckBox);
        checkbox3 = new CheckBox("Pepperoni");
        checkbox3.setOnAction(this::processCheckBox);
        checkbox4 = new CheckBox("Onion");
        checkbox4.setOnAction(this::processCheckBox);
        checkbox5 = new CheckBox("Sausage");
        checkbox5.setOnAction(this::processCheckBox);
        checkbox6 = new CheckBox("Anchovies");
        checkbox6.setOnAction(this::processCheckBox);
        //layout container 4 hbox, 2 vbox?
        HBox hSet1 = new HBox(checkbox1, checkbox2);
        hSet1.setAlignment(Pos.CENTER);
        HBox hSet2 = new HBox(checkbox3, checkbox4);
        hSet2.setAlignment(Pos.CENTER);
        HBox hSet3 = new HBox(checkbox5, checkbox6);
        hSet3.setAlignment(Pos.CENTER);
        HBox userCost = new HBox(text);
        userCost.setAlignment(Pos.CENTER);
        VBox vSet1 = new VBox(hSet1,hSet2,hSet3,userCost);
        vSet1.setAlignment(Pos.CENTER);
        setSpacing(20);
        getChildren().addAll(vSet1);
    }
    public void processCheckBox(ActionEvent event)
    {
//        if(checkbox1.isSelected() || checkbox2.isSelected() || checkbox3.isSelected() || checkbox4.isSelected() || checkbox5.isSelected() || checkbox6.isSelected())
//        {
//            cost = cost + 0.50;
//            text.setText("Pizza Cost:" + cost);
//        }
//        else if(!checkbox1.isSelected() || !checkbox2.isSelected() || !checkbox3.isSelected() || !checkbox4.isSelected() || !checkbox5.isSelected() || !checkbox6.isSelected())
//        {
//            cost = cost - 0.50;
//            text.setText("Pizza Cost:" + cost);
//        }
       if(event.getSource()==checkbox1)
       {
           if(checkbox1.isSelected())
           {
               cost = cost + 0.50;
               text.setText("Pizza Cost:" + cost);
           }
           else
           {
               cost = cost - 0.50;
               text.setText("Pizza Cost:" + cost);
           }
       }
        if(event.getSource()==checkbox2)
        {
            if(checkbox2.isSelected())
            {
                cost = cost + 0.50;
                text.setText("Pizza Cost:" + cost);
            }
            else
            {
                cost = cost - 0.50;
                text.setText("Pizza Cost:" + cost);
            }
        }
        if(event.getSource()==checkbox3)
        {
            if(checkbox3.isSelected())
            {
                cost = cost + 0.50;
                text.setText("Pizza Cost:" + cost);
            }
            else
            {
                cost = cost - 0.50;
                text.setText("Pizza Cost:" + cost);
            }
        }
        if(event.getSource()==checkbox4)
        {
            if(checkbox4.isSelected())
            {
                cost = cost + 0.50;
                text.setText("Pizza Cost:" + cost);
            }
            else
            {
                cost = cost - 0.50;
                text.setText("Pizza Cost:" + cost);
            }
        }
        if(event.getSource()==checkbox5)
        {
            if(checkbox5.isSelected())
            {
                cost = cost + 0.50;
                text.setText("Pizza Cost:" + cost);
            }
            else
            {
                cost = cost - 0.50;
                text.setText("Pizza Cost:" + cost);
            }
        }
        if(event.getSource()==checkbox6)
        {
            if(checkbox6.isSelected())
            {
                cost = cost + 0.50;
                text.setText("Pizza Cost:" + cost);
            }
            else
            {
                cost = cost - 0.50;
                text.setText("Pizza Cost:" + cost);
            }
        }
    }
}

问题是您正在根据每个复选框重新计算整个价格,然后您应该从每个操作事件的基本成本开始:

public void processCheckBox(ActionEvent event)
{
    cost = 10.00;
    int toppings = checkbox1.isSelected() ? 1 : 0 +
                   checkbox2.isSelected() ? 1 : 0 +
                   checkbox3.isSelected() ? 1 : 0 +
                   checkbox4.isSelected() ? 1 : 0 +
                   checkbox5.isSelected() ? 1 : 0 +
                   checkbox6.isSelected() ? 1 : 0;
    cost = cost + (toppings * 0.50);
    text.setText("Pizza Cost:" + cost);
}

问题是您正在根据每个复选框重新计算整个价格,然后您应该从每个操作事件的基本成本开始:

public void processCheckBox(ActionEvent event)
{
    cost = 10.00;
    int toppings = checkbox1.isSelected() ? 1 : 0 +
                   checkbox2.isSelected() ? 1 : 0 +
                   checkbox3.isSelected() ? 1 : 0 +
                   checkbox4.isSelected() ? 1 : 0 +
                   checkbox5.isSelected() ? 1 : 0 +
                   checkbox6.isSelected() ? 1 : 0;
    cost = cost + (toppings * 0.50);
    text.setText("Pizza Cost:" + cost);
}

对了,谢谢我已经修复了它(我想)上面会更新使用的三元运算符很酷,谢谢你的输入!您还可以泛化更新,这样,如果使用简单的强制转换添加另一个复选框,则不需要增加该方法:public void processCheckBox(ActionEvent事件){if(((checkbox)event.getSource()).isSelected(){cost=cost+0.50;}else{cost=cost-0.50;}text.setText(“Pizza cost:+cost”)}对了,谢谢我已经修复了它(我想)上面会更新使用的三元运算符很酷,谢谢你的输入!您还可以泛化更新,这样,如果使用简单的强制转换添加另一个复选框,则不需要增加该方法:public void processCheckBox(ActionEvent事件){if(((checkbox)event.getSource()).isSelected(){cost=cost+0.50;}else{cost=cost-0.50;}text.setText(“Pizza cost:+cost”)}不相关:布局意味着要使用(未扩展)不相关:布局意味着要使用(未扩展)