当 JavaFX 代码为 运行 时,应用程序将不会显示

Application will not display when JavaFX code is ran

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

//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: 5");
        studentAdmissionButton = new RadioButton("Student Admission: 5");

        // 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: ");
        eCommerceButton = new RadioButton("Introduction to E-commerce: 5");
        webFutureButton = new RadioButton("The Future of the Web: 5");
        advancedJavaButton = new RadioButton("Advanced Java Programming: 5");
        securityButton = new RadioButton("Network Security: 5");

        // 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.");
                }
            }
        }
    }
}

感谢您的宝贵时间。我期待了解我所忽视的东西。

您没有初始化 totalChargesLabel

在将其添加到 VBox 之前将其初始化为空 Label:

totalChargesLabel = new Label();