我正在为类(class)编写一个程序,它要求我使用简单的 JavaFX GUI 计算冰淇淋订单的成本。
我在类级别声明了两个 boolean 值,nuts
和 cherries
并尝试使用两个复选框设置它们的值,例如: checkboxNuts.isSelected()
。
我正在尝试将复选框的值传递给方法 showCalculations()
但无论选择什么,我的两个 boolean 值始终为 false。
我该如何编码才能使我的计算反射(reflect)我的复选框的状态?
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();
}
}
最佳答案
您甚至在展示场景之前就阅读了这些值。这意味着 boolean 字段仍然包含初始值,即 false
。
读取初始化计算的事件中的值。
顺便说一句:考虑将浇头存储在数据结构中。这将允许您仅指定数据并基于此数据创建 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();
}
关于java - 如何使用 JavaFX 将复选框的值传递给另一个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49803077/