如何在 JavaFX 中的 windows 之间传递对象

How to pass an object between windows in JavaFX

我是 java 的新手,需要帮助来理解对象。我正在尝试在 javaFx 中创建一个应用程序,但在 windows 之间传递对象时遇到问题。当前,有一个登录 window,如果通过检查数据库登录详细信息正确,则仪表板 window 打开。但是,我现在需要在仪表板 window 中按下登录按钮时刚刚创建的 BackendInterface 对象从 BackendInterface class 调用其方法。我现在所拥有的给出了一个空指针异常。

将构造函数添加到 LoginController class 通过 this.backendInterface 会产生 fxml 加载异常

所以我的问题是如何将实例化对象传递给另一个 window 以使用它的方法?

主要

    public class MainGui extends Application {

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

            Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
            primaryStage.setTitle("Login");
            primaryStage.setScene(new Scene(root));
            primaryStage.show();

        }


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

登录按钮的LoginController

    public class LoginController {

        BackendInterface backendInterface;

        @FXML
        TextField username;

        @FXML
        PasswordField password;

        @FXML
        Button loginButton;

        @FXML
        Label loginLabel;

        @FXML
        public void loginButtonPress(){

            if (username.getText().isEmpty() == true || password.getText().isEmpty() == true ) {

                loginLabel.setText("Please enter data in the fields below");

            } else {

                //initialises backend interface with username and password
                backendInterface = new BackendInterface(username.getText(), password.getText().toCharArray());

                //opens a connection to the database
                backendInterface.openConnection();

                if (backendInterface.getConnectionResponse() == "success"){

                    //return and print response
                    System.out.println(backendInterface.getConnectionResponse());

                    //directs the user to the dashboard after successful login
                    try{

                        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("dashboard.fxml"));
                        Parent root1 = (Parent) fxmlLoader.load();
                        Stage stage = new Stage();
                        stage.initModality(Modality.APPLICATION_MODAL);
                        stage.setTitle("Welcome " + username.getText());
                        stage.setScene(new Scene(root1));
                        stage.show();

                        //Closes the login screen window
                        Stage stage2 = (Stage) loginButton.getScene().getWindow();
                        stage2.hide();

                    } catch (Exception e){
                        e.printStackTrace();
                    }

                } else {
                    //warns user of invalid login
                    loginLabel.setText(backendInterface.getConnectionResponse());
                }
            }
        }
    }

仪表板上按钮的 DashboardController

    public class DashboardController {

        @FXML
        public void doStuff(){

            LoginController loginController = new LoginController();

            loginController.backendInterface.printSomething();


        }

    }

基于 Clayns 响应的更新解决方案:

首先控制器加载新页面并传递对象

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("dashboard.fxml"));
loader.load();
Parent p = loader.getRoot();
Stage stage = new Stage();
stage.setScene(new Scene(p));

DashboardController dashboardController = loader.getController();
dashboardController.setBackendInterface(backendInterface);

第二个控制器检索对象方法

public void setBackendInterface(BackendInterface backendInterface) {
        this.backendInterface = backendInterface;
    }

您正在 DashboardController 中创建一个与用于登录的那个无关的新 LoginController

您应该为您的 DashboardController 提供一个设置 BackendInterface 的方法,然后在 LoginController 中使用 fxmlLoader.getController() 获取 DashboardController 并将 BackendInterface 传递给它。

编辑: fabian 的回答也是一样的