如何在javafx中的不同类之间切换场景?

时间:2020-05-04 05:06:39

标签: java javafx

以下是此类的代码:MainMenu和SupplierRegistration。我在MainMenu类中只有一个名为“添加新的供应商”的按钮,我想添加功能,这样,一旦单击此按钮,我便可以移至SupplierRegistration屏幕,并且当我在vendorRegistration屏幕我可以切换到MainMenu屏幕,但要求是两个屏幕不能同时打开。我正在努力实现此功能。有人可以提供我的代码吗,我将非常感谢,谢谢!

public class MainMenu extends Application {

    Scene mainMenuScene;
    Button addSupplierBtn;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Main Menu");

        addSupplierBtn = new Button("Add a new Supplier");

        // the layout.
        StackPane layout = new StackPane();
        layout.getChildren().add(addSupplierBtn);

        mainMenuScene = new Scene(layout, 300, 250);
        primaryStage.setScene(mainMenuScene);
        primaryStage.show();

    }
}

// now the supplierRegistration class.


public class supplierRegistration extends Application {

    Stage window;

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;
        window.setTitle("Supplier Registration Form");
        GridPane gridPane = registrationPane();
        addUI(gridPane);

        Scene scene = new Scene(gridPane, 800, 500);
        window.setScene(scene);
        window.show();

    }

    public static void main(String[] args) {
        launch(args);

    }

    // now the layout.
    private GridPane registrationPane() {

        // declare and initliase a new GridPane layout.
        GridPane gridPane = new GridPane();

        // position the pane at the center.
        gridPane.setAlignment(Pos.CENTER);

        // set padding
        gridPane.setPadding(new Insets(40, 40, 40, 40));
        gridPane.setHgap(10);
        gridPane.setVgap(10);

        // add column constraints
        ColumnConstraints columnone = new ColumnConstraints(100, 100, Double.MAX_VALUE);
        columnone.setHalignment(HPos.RIGHT);

        ColumnConstraints columntwo = new ColumnConstraints(200, 200, Double.MAX_VALUE);
        columntwo.setHgrow(Priority.ALWAYS);
        gridPane.getColumnConstraints().addAll(columnone, columntwo);
        return gridPane;

    }

    private void addUI(GridPane gridPane) {
        // add header
        Label header = new Label("Register Supplier");
        header.setFont(Font.font("Arial Nova Light", FontWeight.BOLD, 22));
        gridPane.add(header, 0, 0, 3, 1);
        GridPane.setHalignment(header, HPos.CENTER);
        GridPane.setMargin(header, new Insets(20, 0, 20, 0));

        // full name label.
        Label fullName = new Label("Full Name");
        fullName.setMaxWidth(222); // 1240
        gridPane.add(fullName, 0, 1);

        // name textfield.
        TextField inputName = new TextField();
        inputName.setPrefHeight(28);
        inputName.setMaxWidth(222);
        gridPane.add(inputName, 1, 1);

        // addressline1 label.
        Label addressline1 = new Label("Address Line 1");
        addressline1.setMaxWidth(164);
        gridPane.add(addressline1, 0, 2);

        // addressline 1 textfield.
        TextField inputAddressLine1 = new TextField();
        inputAddressLine1.setPrefHeight(28);
        inputAddressLine1.setMaxWidth(222);
        gridPane.add(inputAddressLine1, 1, 2);

        // addressline 2 label.
        Label addressline2 = new Label("Address Line 2");
        addressline2.setMaxWidth(164);
        gridPane.add(addressline2, 0, 3);

        // addressline 2 textfield.
        TextField inputAddressLine2 = new TextField();
        inputAddressLine2.setPrefHeight(28);
        inputAddressLine2.setMaxWidth(222);
        gridPane.add(inputAddressLine2, 1, 3);

        // postcode label.
        Label postCode = new Label("Post Code");
        postCode.setMaxWidth(164);
        gridPane.add(postCode, 0, 4);

        // textfield for postcode
        TextField inputPostCode = new TextField();
        inputPostCode.setPrefHeight(28);
        inputPostCode.setMaxWidth(108);
        gridPane.add(inputPostCode, 1, 4);

        // email label.
        Label email = new Label("Email");
        email.setMaxWidth(164);
        gridPane.add(email, 0, 5);

        // textfield for email.
        TextField inputEmail = new TextField();
        inputEmail.setPrefHeight(28);
        inputEmail.setMaxWidth(222);
        gridPane.add(inputEmail, 1, 5);

        // label for cell phone number.
        Label mobilePhone = new Label("Mobile Phone");
        mobilePhone.setMaxWidth(164);
        gridPane.add(mobilePhone, 0, 6);

        // textfield for cell number.
        TextField mobileNumber = new TextField();
        mobileNumber.setPrefHeight(28);
        mobileNumber.setMaxWidth(222);
        gridPane.add(mobileNumber, 1, 6);

        // submit button.
        Button submitBtn = new Button("Submit");
        submitBtn.setId("submitBtn");
        submitBtn.setPrefHeight(36);
        submitBtn.setDefaultButton(true);
        submitBtn.setPrefWidth(82);
        gridPane.add(submitBtn, 0, 7, 2, 1);
        GridPane.setHalignment(submitBtn, HPos.LEFT);
        GridPane.setMargin(submitBtn, new Insets(30, 0, 30, 0));

        // back button which will take the user to the main menu.
        Button backBtn = new Button("Back");
        backBtn.setId("backBtn");
        backBtn.setPrefHeight(36);
        backBtn.setDefaultButton(true);
        backBtn.setPrefWidth(82);
        gridPane.add(backBtn, 0, 8, 2, 1);
        GridPane.setHalignment(backBtn, HPos.RIGHT);
        GridPane.setMargin(backBtn, new Insets(30, 0, 30, 0));

        submitBtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if (inputEmail.getText().isEmpty() && mobileNumber.getText().isEmpty()) {
                    alertBox(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Error",
                            "Please provide either a valid email address or contact no");
                    return;

                } else if (inputName.getText().isEmpty()) {
                    alertBox(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Error",
                            "Name Field cannot be empty!");
                    return;

                } else if (inputAddressLine1.getText().isEmpty() && inputAddressLine2.getText().isEmpty()) {
                    alertBox(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Error",
                            "Address field cannot be empty!");
                    return;

                } else if (postCode.getText().isEmpty()) {
                    alertBox(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Error",
                            "Post Code cannot be empty");

                    return;

                }

                alertBox(Alert.AlertType.CONFIRMATION, gridPane.getScene().getWindow(), "Confirmation",
                        "Supplier has been successfully registered!");

            }

        });
        // backBtn.setOnAction(e -> window.setScene(mainMenuScene));

    }

    private void alertBox(Alert.AlertType alertType, Window admin, String title, String message) {
        Alert alert = new Alert(alertType);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.initOwner(admin);
        alert.show();

    }
}

1 个答案:

答案 0 :(得分:0)

一个程序应该只有一个类Application。 Remove扩展了SupplierRegistration类的Aplication并将方法设置为public。

在MainMenu中:

Button addSupplierBtn = new Button();
addSupplierBtn.setOnAction(event -> {
        SupplierRegistration regis = new SupplierRegistration();
        GridPane gridPane = regis.registrationPane();
        regis.addUI(gridPane);
        primaryStage.setTitle("Supplier Registration Form");
        primaryStage.setScene(new Scene(gridPane));
    });

所以,另一种方法是使用param将所有内容放入构造函数中:window和show方法

public SupplierRegistration( Stage stage) {
    this.window= stage;
}
public void show(){
     GridPane gridPane =   registrationPane();
      addUI(gridPane);
      window.setTitle("Supplier Registration Form");
      window.setScene(new Scene(gridPane));
}