簡體   English   中英

如何將用戶對象從一個控制器傳遞到另一個JavaFX控制器?

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

很難解釋,但我會嘗試。 我在場景1控制器中有一個User對象,並且我希望將此用戶傳遞給場景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()是不同類中檢查有效用戶的方法; 這是第二個控制器:

@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”傳遞給控制器​​2中的passUser()方法。在控制器2中,方法passUser()中,將私有變量loginUser設置為該方法已接收的用戶。 然后,我在initialize()中打印出此User對象。 但是控制器2中的initialize()打印出“空”,而不是我在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文件與我不提供的代碼無關。

當然是班級用戶

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM