如何将用户对象从一个控制器传递到不同的 JavaFX 控制器?

How to pass the User object from one Controller to the different JavaFX Controller?

很难解释,但我会尝试。我在场景 #1 控制器中有一个用户对象,我希望将此用户传递给场景 #2 控制器。 这是第一个控制器:

            @FXML
            private TextField password;
            @FXML
            private TextField username;
            private String id,pass;
            private User loginUser;

            DisplayController controller = new DisplayController();
            id = username.getText();
            pass = password.getText();
            loginUser = databaseStack.checkUser(id,pass);
            controller.passUser(loginUser);
            System.out.println(loginUser);
            Parent root = null;
            try {
                root = FXMLLoader.load(Main.class.getResource("javafx3.fxml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Scene signUpScene = new Scene(root);
            Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
            window.setScene(signUpScene);
            window.show();

*databaseStack.checkUser() 是不同 class 中检查有效用户的方法; 这是第二个控制器:

@FXML
private Button findFriend;
@FXML
private Label fullName;
@FXML
private Label eMail;
@FXML
private Label hobbies;
@FXML
private Label major;
private User loginUser;
public void initialize(){
    System.out.println(loginUser);
    findFriend.setStyle("-fx-background-color: \n" +
            "        linear-gradient(#ffd65b, #e68400),\n" +
            "        linear-gradient(#ffef84, #f2ba44),\n" +
            "        linear-gradient(#ffea6a, #efaa22),\n" +
            "        linear-gradient(#ffe657 0%, #f8c202 50%, #eea10b 100%),\n" +
            "        linear-gradient(from 0% 0% to 15% 50%, rgba(255,255,255,0.9), rgba(255,255,255,0));\n" +
            "    -fx-background-radius: 30;\n" +
            "    -fx-background-insets: 0,1,2,3,0;\n" +
            "    -fx-text-fill: #654b00;\n" +
            "    -fx-font-weight: bold;\n" +
            "    -fx-font-size: 20px;\n" +
            "    -fx-padding: 10 20 10 20;");
    findFriend.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            Parent root = null;
            try {
                root = FXMLLoader.load(Main.class.getResource("javafx4.fxml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Scene signUpScene = new Scene(root);
            Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
            window.setScene(signUpScene);
            window.show();
        }
    });
}
public void passUser(User user){
    loginUser = user;
}

在控制器 1 中,我将用户 "loginUser" 传递给控制器​​中的 passUser() 方法 2.In 控制器 2 在方法 passUser() 中,我将私有变量 loginUser 设置为用户方法已收到。然后我在 initialize() 中打印出这个 User 对象。但是控制器 2 中的 initialize() 打印出 "null" 而不是我在 passUser() 中传递的用户。这可能是因为当我切换场景时它会重置所有变量。我如何解决它?如何接收传递给 passUser() 方法中的局部变量的 User 对象? 抱歉解释不好,真的很难为我解释这个问题。 感谢您的时间和努力!

这是控制器之间直接通信的示例。

public class ControllerA {
    @FXML
    private TextField usernameTextField;

    @FXML
    private Button loginButton;

    private Stage stage;

    @FXML
    private void initialize() {
        loginButton.setOnAction(event -> {
            try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("scene_2.fxml"));
                Parent parent = loader.load();

                ControllerB controllerB = loader.getController();
                controllerB.setUser(new User(usernameTextField.getText()));

                stage.setScene(new Scene(parent));

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

    public void setStage(Stage stage) {
        this.stage = stage;
    }
}

public class ControllerB {

    private User user;

    @FXML
    private Label wellcomeLabel;

    @FXML
    private void initialize() {

    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
        wellcomeLabel.setText("Wellcome " + user.getName());
    }
}

public class MainStage extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("scene_1.fxml"));
        Parent parent = loader.load();

        ControllerA controllerA = loader.getController();
        controllerA.setStage(primaryStage);

        primaryStage.setTitle("Demo");
        primaryStage.setScene(new Scene(parent));
        primaryStage.show();
    }

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

如果要使用调解器,事情会是这样的:

public class Mediator {
    private static Mediator INSTANCE;

    private Stage stage;
    private User user;

    public static Mediator getInstance() {
        if(INSTANCE == null) {
            INSTANCE = new Mediator();
        }
        return INSTANCE;
    }

    private Mediator() {
    }

    public Stage getStage() {
        return stage;
    }

    public void setStage(Stage stage) {
        this.stage = stage;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

public class MainStage extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Mediator.getInstance().setStage(primaryStage);

        Parent parent = FXMLLoader.load(getClass().getResource("scene_1.fxml"));

        primaryStage.setTitle("Demo");
        primaryStage.setScene(new Scene(parent));
        primaryStage.show();
    }

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

public class ControllerA {
    @FXML
    private TextField usernameTextField;

    @FXML
    private Button loginButton;

    @FXML
    private void initialize() {
        loginButton.setOnAction(event -> {
            try {
                Mediator.getInstance().setUser(new User(usernameTextField.getText()));
                Parent parent = FXMLLoader.load(getClass().getResource("scene_2.fxml"));

                Stage stage = Mediator.getInstance().getStage();
                stage.setScene(new Scene(parent));

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

public class ControllerB {

    @FXML
    private Label wellcomeLabel;

    @FXML
    private void initialize() {
        User user = Mediator.getInstance().getUser();
        wellcomeLabel.setText("Wellcome " + user.getName());
    }

}

FXML 文件与我没有提供的代码无关。

当然还有class用户

public class User {
    private String name;

    public User() {
    }

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}