如何使用JavaFX将复选框的值传递给另一个方法?
我正在为这个类编写一个程序,它要求我使用一个简单的JavaFXGUI计算一份冰淇淋订单的成本 我在类级别声明了两个布尔值,如何使用JavaFX将复选框的值传递给另一个方法?,java,javafx,checkbox,boolean,Java,Javafx,Checkbox,Boolean,我正在为这个类编写一个程序,它要求我使用一个简单的JavaFXGUI计算一份冰淇淋订单的成本 我在类级别声明了两个布尔值,nuts和cherries,并尝试使用两个复选框设置它们的值,例如:checkboxNuts.isSelected() 我试图将复选框的值传递给方法showcumptions(),但无论选择什么,我的两个布尔值都始终为false 如何对其进行编码以使计算反映复选框的状态 package capstone; import javafx.application.Applicati
nuts
和cherries
,并尝试使用两个复选框设置它们的值,例如:checkboxNuts.isSelected()
我试图将复选框的值传递给方法showcumptions()
,但无论选择什么,我的两个布尔值都始终为false
如何对其进行编码以使计算反映复选框的状态
package capstone;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Capstone extends Application {
private boolean nuts = false;
private boolean cherries = false;
public static void main(String[] args) {
launch(args);
}
@Override public void start(Stage stage) {
stage.setTitle("Ice Cream - Swejkoski");
Scene scene = new Scene(new Group(), 750, 125);
scene.setFill(Color.GHOSTWHITE);
ToggleGroup group = new ToggleGroup();
final RadioButton rbChocolate = new RadioButton("Chocolate");
rbChocolate.setToggleGroup(group);
rbChocolate.setSelected(true);
final RadioButton rbVanilla = new RadioButton("Vanilla");
rbVanilla.setToggleGroup(group);
final RadioButton rbStrawberry = new RadioButton("Strawberry");
rbStrawberry.setToggleGroup(group);
TitledPane gridTitlePaneFlavors = new TitledPane();
GridPane gridFlavors = new GridPane();
gridFlavors.setVgap(10);
gridFlavors.setHgap(4);
gridFlavors.setPadding(new Insets(1, 1, 1, 1));
gridFlavors.add(rbChocolate , 0, 0);
gridFlavors.add(rbVanilla , 1, 0);
gridFlavors.add(rbStrawberry , 2, 0);
gridTitlePaneFlavors.setText("Flavors");
gridTitlePaneFlavors.setContent(gridFlavors);
final CheckBox checkboxNuts = new CheckBox("Nuts");
final CheckBox checkboxCherries = new CheckBox("Cherries");
if (checkboxNuts.isSelected()) {
nuts = true;
}
else {
nuts = false;
}
if (checkboxCherries.isSelected()) {
cherries = true;
}
else {
cherries = false;
}
TitledPane gridTitlePaneToppings = new TitledPane();
GridPane gridToppings = new GridPane();
gridToppings.setVgap(10);
gridToppings.setHgap(4);
gridToppings.setPadding(new Insets(1, 1, 1, 1));
gridToppings.add(checkboxNuts , 0, 0);
gridToppings.add(checkboxCherries , 1, 0);
gridTitlePaneToppings.setText("Toppings");
gridTitlePaneToppings.setContent(gridToppings);
final Button buttonCalculate = new Button("Calculate Cost");
buttonCalculate.setOnAction((event) -> {
showCalculations();
System.out.println("Button Action");
});
final Button buttonSave = new Button("Save");
final Button buttonRestore = new Button("Restore");
TitledPane gridTitlePaneOrder = new TitledPane();
GridPane gridOrder = new GridPane();
gridOrder.setVgap(10);
gridOrder.setHgap(4);
gridOrder.setPadding (new Insets(1, 1, 1, 1));
gridOrder.add(buttonCalculate , 0, 0);
gridOrder.add(buttonSave , 1, 0);
gridOrder.add(buttonRestore , 2, 0);
gridTitlePaneOrder.setText("Order");
gridTitlePaneOrder.setContent(gridOrder);
HBox hbox = new HBox(10);
hbox.setPadding(new Insets(20, 0, 0, 20));
hbox.getChildren().add(gridTitlePaneFlavors);
hbox.getChildren().add(gridTitlePaneToppings);
hbox.getChildren().add(gridTitlePaneOrder);
Group root = (Group)scene.getRoot();
root.getChildren().add(hbox);
stage.setScene(scene);
stage.show();
}
public void showCalculations() {
final double salesTax = 0.06;
final double flavorPrice = 2.25;
final double nutsPrice = 0.5;
final double cherriesPrice = 0.5;
double addonsPrice = 0;
if (nuts == true) {
addonsPrice += nutsPrice;
}
else if (nuts == false) {
addonsPrice += 0;
}
if (cherries == true) {
addonsPrice += cherriesPrice;
}
else if (cherries == false) {
addonsPrice += 0;
}
double mainTotal = flavorPrice + addonsPrice + ((flavorPrice + addonsPrice) * salesTax);
double order = flavorPrice + addonsPrice;
double tax = (flavorPrice + addonsPrice) * salesTax;
double total = mainTotal;
Stage calculationStage = new Stage();
calculationStage.setTitle("Ice Cream Calculations");
Scene calculationScene = new Scene(new Group(), 400, 400);
calculationScene.setFill(Color.GHOSTWHITE);
final Label labelMainTotal = new Label("Total: $" + mainTotal);
final Label labelOrder = new Label("Order: $" + order);
final Label labelTax = new Label("Tax: $" + tax);
final Label labelTotal = new Label("Total: $" + total);
GridPane gridCalculation = new GridPane();
gridCalculation.setVgap(10);
gridCalculation.setHgap(4);
gridCalculation.add(labelMainTotal , 0, 0);
gridCalculation.add(labelOrder, 0, 2);
gridCalculation.add(labelTax, 0, 3);
gridCalculation.add(labelTotal, 0 , 4);
HBox calculationhbox = new HBox(10);
calculationhbox.setPadding(new Insets(20, 0, 0, 20));
calculationhbox.getChildren().setAll(gridCalculation);
Group calculationRoot = (Group)calculationScene.getRoot();
calculationRoot.getChildren().add(calculationhbox);
calculationStage.setScene(calculationScene);
calculationStage.show();
}
}
这里的关键是收听
复选框
selectedProperty
值更改。当值更改时,更新布尔变量。下面的代码演示了如何收听复选框的selectedProperty
替换:
if (checkboxNuts.isSelected()) {
nuts = true;
}
else {
nuts = false;
}
if (checkboxCherries.isSelected()) {
cherries = true;
}
else {
cherries = false;
}
与:
完整应用程序:
在显示场景之前,您已经读取了这些值。这意味着布尔字段仍然包含初始值,即false
读取初始化计算的事件中的值
BTW:考虑将配料存储在数据结构中。这将允许您仅指定数据并基于此数据创建UI。这使得以后更容易调整数据
简化示例:
@Override
public void start(Stage primaryStage) {
Topping[] toppings = new Topping[] {
new Topping("Nuts", 0.5),
new Topping("Cherries", 0.5),
new Topping("Jelly", 0.75)
};
// create ui based on toppings data
Map<Topping, CheckBox> toppingsBoxes = new HashMap<>();
VBox toppingsPane = new VBox();
for (Topping topping : toppings) {
CheckBox cb = new CheckBox(topping.getName());
toppingsBoxes.put(topping, cb);
toppingsPane.getChildren().add(cb);
}
Button btn = new Button("Calculate Price");
btn.setOnAction((ActionEvent event) -> {
double price = 0;
// loop reads the selected state of the CheckBoxes
for (Map.Entry<Topping, CheckBox> entry : toppingsBoxes.entrySet()) {
if (entry.getValue().isSelected()) {
price += entry.getKey().getPrice();
}
}
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setContentText(String.format("Price: %.2f", price));
alert.initOwner(primaryStage);
alert.showAndWait();
});
Scene scene = new Scene(new HBox(toppingsPane, btn));
primaryStage.setScene(scene);
primaryStage.show();
}
@覆盖
公共无效开始(阶段primaryStage){
浇头[]浇头=新浇头[]{
新面层(“螺母”,0.5),
新浇头(“樱桃”,0.5),
新浇头(“果冻”,0.75)
};
//基于浇头数据创建用户界面
Map-toppingsboxs=new-HashMap();
VBox toppingsPane=新的VBox();
用于(浇头浇头:浇头){
复选框cb=新复选框(topping.getName());
浇头盒。放置(浇头,cb);
toppingsPane.getChildren().add(cb);
}
按钮btn=新按钮(“计算价格”);
btn.setOnAction((ActionEvent事件)->{
双倍价格=0;
//循环读取复选框的选定状态
对于(Map.Entry:toppingsboxs.entrySet()){
if(entry.getValue().isSelected()){
price+=entry.getKey().getPrice();
}
}
Alert Alert=新警报(Alert.AlertType.INFORMATION);
alert.setContentText(String.format(“Price:%.2f”,Price));
alert.initOwner(primaryStage);
alert.showAndWait();
});
场景=新场景(新HBox(toppingsPane,btn));
初级阶段。场景(场景);
primaryStage.show();
}
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Capstone extends Application
{
private boolean nuts = false;
private boolean cherries = false;
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage stage)
{
stage.setTitle("Ice Cream - Swejkoski");
Scene scene = new Scene(new Group(), 750, 125);
scene.setFill(Color.GHOSTWHITE);
ToggleGroup group = new ToggleGroup();
final RadioButton rbChocolate = new RadioButton("Chocolate");
rbChocolate.setToggleGroup(group);
rbChocolate.setSelected(true);
final RadioButton rbVanilla = new RadioButton("Vanilla");
rbVanilla.setToggleGroup(group);
final RadioButton rbStrawberry = new RadioButton("Strawberry");
rbStrawberry.setToggleGroup(group);
TitledPane gridTitlePaneFlavors = new TitledPane();
GridPane gridFlavors = new GridPane();
gridFlavors.setVgap(10);
gridFlavors.setHgap(4);
gridFlavors.setPadding(new Insets(1, 1, 1, 1));
gridFlavors.add(rbChocolate, 0, 0);
gridFlavors.add(rbVanilla, 1, 0);
gridFlavors.add(rbStrawberry, 2, 0);
gridTitlePaneFlavors.setText("Flavors");
gridTitlePaneFlavors.setContent(gridFlavors);
final CheckBox checkboxNuts = new CheckBox("Nuts");
checkboxNuts.selectedProperty().addListener((obs, oldValue, newValue) -> {
nuts = newValue;
});
final CheckBox checkboxCherries = new CheckBox("Cherries");
checkboxCherries.selectedProperty().addListener((obs, oldValue, newValue) -> {
cherries = newValue;
});
TitledPane gridTitlePaneToppings = new TitledPane();
GridPane gridToppings = new GridPane();
gridToppings.setVgap(10);
gridToppings.setHgap(4);
gridToppings.setPadding(new Insets(1, 1, 1, 1));
gridToppings.add(checkboxNuts, 0, 0);
gridToppings.add(checkboxCherries, 1, 0);
gridTitlePaneToppings.setText("Toppings");
gridTitlePaneToppings.setContent(gridToppings);
final Button buttonCalculate = new Button("Calculate Cost");
buttonCalculate.setOnAction((event) -> {
showCalculations();
System.out.println("Button Action");
});
final Button buttonSave = new Button("Save");
final Button buttonRestore = new Button("Restore");
TitledPane gridTitlePaneOrder = new TitledPane();
GridPane gridOrder = new GridPane();
gridOrder.setVgap(10);
gridOrder.setHgap(4);
gridOrder.setPadding(new Insets(1, 1, 1, 1));
gridOrder.add(buttonCalculate, 0, 0);
gridOrder.add(buttonSave, 1, 0);
gridOrder.add(buttonRestore, 2, 0);
gridTitlePaneOrder.setText("Order");
gridTitlePaneOrder.setContent(gridOrder);
HBox hbox = new HBox(10);
hbox.setPadding(new Insets(20, 0, 0, 20));
hbox.getChildren().add(gridTitlePaneFlavors);
hbox.getChildren().add(gridTitlePaneToppings);
hbox.getChildren().add(gridTitlePaneOrder);
Group root = (Group) scene.getRoot();
root.getChildren().add(hbox);
stage.setScene(scene);
stage.show();
}
public void showCalculations()
{
final double salesTax = 0.06;
final double flavorPrice = 2.25;
final double nutsPrice = 0.5;
final double cherriesPrice = 0.5;
double addonsPrice = 0;
if (nuts == true) {
addonsPrice += nutsPrice;
}
else if (nuts == false) {
addonsPrice += 0;
}
if (cherries == true) {
addonsPrice += cherriesPrice;
}
else if (cherries == false) {
addonsPrice += 0;
}
double mainTotal = flavorPrice + addonsPrice + ((flavorPrice + addonsPrice) * salesTax);
double order = flavorPrice + addonsPrice;
double tax = (flavorPrice + addonsPrice) * salesTax;
double total = mainTotal;
Stage calculationStage = new Stage();
calculationStage.setTitle("Ice Cream Calculations");
Scene calculationScene = new Scene(new Group(), 400, 400);
calculationScene.setFill(Color.GHOSTWHITE);
final Label labelMainTotal = new Label("Total: $" + mainTotal);
final Label labelOrder = new Label("Order: $" + order);
final Label labelTax = new Label("Tax: $" + tax);
final Label labelTotal = new Label("Total: $" + total);
GridPane gridCalculation = new GridPane();
gridCalculation.setVgap(10);
gridCalculation.setHgap(4);
gridCalculation.add(labelMainTotal, 0, 0);
gridCalculation.add(labelOrder, 0, 2);
gridCalculation.add(labelTax, 0, 3);
gridCalculation.add(labelTotal, 0, 4);
HBox calculationhbox = new HBox(10);
calculationhbox.setPadding(new Insets(20, 0, 0, 20));
calculationhbox.getChildren().setAll(gridCalculation);
Group calculationRoot = (Group) calculationScene.getRoot();
calculationRoot.getChildren().add(calculationhbox);
calculationStage.setScene(calculationScene);
calculationStage.show();
}
}
@Override
public void start(Stage primaryStage) {
Topping[] toppings = new Topping[] {
new Topping("Nuts", 0.5),
new Topping("Cherries", 0.5),
new Topping("Jelly", 0.75)
};
// create ui based on toppings data
Map<Topping, CheckBox> toppingsBoxes = new HashMap<>();
VBox toppingsPane = new VBox();
for (Topping topping : toppings) {
CheckBox cb = new CheckBox(topping.getName());
toppingsBoxes.put(topping, cb);
toppingsPane.getChildren().add(cb);
}
Button btn = new Button("Calculate Price");
btn.setOnAction((ActionEvent event) -> {
double price = 0;
// loop reads the selected state of the CheckBoxes
for (Map.Entry<Topping, CheckBox> entry : toppingsBoxes.entrySet()) {
if (entry.getValue().isSelected()) {
price += entry.getKey().getPrice();
}
}
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setContentText(String.format("Price: %.2f", price));
alert.initOwner(primaryStage);
alert.showAndWait();
});
Scene scene = new Scene(new HBox(toppingsPane, btn));
primaryStage.setScene(scene);
primaryStage.show();
}