简体   繁体   English

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

[英]How to switch scenes among different classes in javafx?

Below is the code for the classes: MainMenu and supplierRegistration.下面是类的代码:MainMenu 和supplierRegistration。 I have only one button in the MainMenu class called "Add a new Supplier", I want to add the functionality such that once this button is clicked I am able to move to the supplierRegistration screen and similarly when I click the back button while on the supplierRegistration screen I can switch to the MainMenu screen but the requirement is that both screens must not be open at the same time.我在 MainMenu class 中只有一个按钮,称为“添加新供应商”,我想添加功能,以便一旦单击此按钮,我就可以移动到供应商注册屏幕,同样,当我在上单击后退按钮时供应商注册屏幕 我可以切换到 MainMenu 屏幕,但要求不能同时打开两个屏幕。 I am struggling to implement this functionality.我正在努力实现这个功能。 Can someone provide me with the code for this, I'll be very grateful, thank you!谁能给我这个代码,我将非常感激,谢谢!

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();

    }
}

A program should have only one class Application.一个程序应该只有一个 class 应用程序。 Remove extends Aplication of SupplierRegistration class and set methods to public.删除 SupplierRegistration class 的扩展应用程序并将方法设置为公共。

And in MainMenu:在 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));
    });

So, Another way is to put everything into the constructor with param is window and method show因此,另一种方法是将所有内容放入构造函数中,参数为 window 和方法显示

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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM