Java-如何在控制器之间传递用户对象

时间:2019-03-21 20:33:11

标签: java oop object javafx

我正在创建一个JavaFX应用程序,并且我有一个User类实例,该实例具有2个变量;用户名和密码。在loginController类上,将创建并验证User对象,然后弹出下一个屏幕。我想使用在homePageController的loginController中创建的相同用户对象,即我想将相同的用户对象传递给下一个控制器。到目前为止,这是我的代码;

这是成功创建用户对象的loginController的代码

public void onClickLogin( ActionEvent event) throws IOException {


        User user = new User(userTextField.getText(), passTextField.getText());

        user.validate(userTextField.getText(), passTextField.getText());
        System.out.println(user.getUsername());

            if(user.getUsername().equals(userTextField.getText()) && user.getPassword().equals(passTextField.getText())) {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/Personell.fxml") );


                Parent root = (Parent) loader.load();

                //here I want to pass the user object to the next controller once the user logins in sucessfuly

                Scene scene2View = new Scene(root);

                Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();

                window.setScene(scene2View);
                window.show();

            }

这是homePage控制器的代码,我需要在loginPage中创建的用户对象:

public class PersonnelController implements Initializable{
// In this class I need to have access to the user Object already created
    @FXML private Button logOutButton;

    @FXML
    public void onClickLogOutButton(ActionEvent event) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/Login.fxml") );

        Parent root = (Parent) loader.load();
        Scene scene2View = new Scene(root);

        Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();

        window.setScene(scene2View);
        window.show();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

}

0 个答案:

没有答案
相关问题