运行javafx代码时不会显示应用程序

rbpvctlc  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(390)

我正在做一个非常小的,简短的应用程序来计算即将召开的会议的费用。当我运行代码直到添加事件处理程序时,应用程序显示良好。一切似乎都在控制中,所以我不确定发生了什么。如有任何见解,将不胜感激。

//JavaFX imports
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ConferenceRegistration extends Application {

    // Create radio buttons for conference event options
    RadioButton generalAdmissionButton, studentAdmissionButton, keynoteDinnerButton, eCommerceButton, webFutureButton,
            advancedJavaButton, securityButton;

    Label labelAdmission, labelOptionalEvents, totalChargesLabel;

    Button totalCharges;

    public static void main(String[] args) {

        // launch the application
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {

        // Label for admission type selection
        labelAdmission = new Label("Please select your Admission type: ");

        // mandatory selection for conference
        generalAdmissionButton = new RadioButton("General Admission: $895");
        studentAdmissionButton = new RadioButton("Student Admission: $495");

        // Create toggle group for either admission group
        ToggleGroup optionalEvents = new ToggleGroup();
        generalAdmissionButton.setToggleGroup(optionalEvents);
        studentAdmissionButton.setToggleGroup(optionalEvents);

        // Label for optional conference events
        labelOptionalEvents = new Label("Please Select All Optional Events You Will Be Attending: ");

        // set values for optional conference events
        keynoteDinnerButton = new RadioButton("Keynote Speech Dinner: $30");
        eCommerceButton = new RadioButton("Introduction to E-commerce: $295");
        webFutureButton = new RadioButton("The Future of the Web: $295");
        advancedJavaButton = new RadioButton("Advanced Java Programming: $395");
        securityButton = new RadioButton("Network Security: $395");

        // Button for calculating total Conference charges
        totalCharges = new Button("Calculate Total");
        totalCharges.setOnAction(new TotalChargesCalculator());

        // create Vbox container and add all labels, buttons
        VBox vbox = new VBox(10, labelAdmission, generalAdmissionButton, studentAdmissionButton, labelOptionalEvents,
                keynoteDinnerButton, eCommerceButton, webFutureButton, advancedJavaButton, securityButton, totalCharges,
                totalChargesLabel);

        // format vbox
        vbox.setAlignment(Pos.CENTER);
        vbox.setPadding(new Insets(20));

        // create and set scene
        Scene scene = new Scene(vbox);
        stage.setTitle("Conference Registration");
        stage.setScene(scene);

        // show stage
        stage.show();

    }

    class TotalChargesCalculator implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent arg0) {

            int result = 0;

            try {

                // check which radio buttons are selected
                if (generalAdmissionButton.isSelected()) {
                    result = result + 895;
                }
                if (studentAdmissionButton.isSelected()) {
                    result = result + 495;
                }
                if (keynoteDinnerButton.isSelected()) {
                    result = result + 295;
                }
                if (eCommerceButton.isSelected()) {
                    result = result + 295;
                }
                if (webFutureButton.isSelected()) {
                    result = result + 295;
                }
                if (advancedJavaButton.isSelected()) {
                    result = result + 395;
                }
                if (securityButton.isSelected()) {
                    result = result + 395;
                }

                totalChargesLabel.setText(String.valueOf(result));

            } catch (Exception e) {
                if (generalAdmissionButton.isSelected() == false || studentAdmissionButton.isSelected() == false) {
                    totalChargesLabel.setText("Please Select Admission Type.");
                }
            }
        }
    }
}

谢谢你的时间。我期待着学习我忽略的东西。

eaf3rand

eaf3rand1#

您没有初始化 totalChargesLabel .
将其初始化为空 Label 在将其添加到 VBox :

totalChargesLabel = new Label();

相关问题