我可以通过logInEvent从fxmlLogIn.fxml中的文本字段设置标签到fxmlMain.fxml。
我的问题是如何从fxmlLogIn.fxml访问相同的值到以后仍会打开的舞台/场景。
我的想法是制作更多可以传递每个舞台/场景数据的课程,但我知道这不是正确的方法。
FxmlControllerLogIn.java
@FXML
private void logInEvent(ActionEvent event)
{
FromUsers fromUsers = new FromUsers (fxTxtUserName.getText());
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/scenes/fxmlMain.fxml"));
loader.load();
Parent mainMenuParent = loader.getRoot();
FxmlControllerMain fxmlcontrollermain = loader.getController();
fxmlcontrollermain.setChosenUser(fromUsers);
Stage logIn = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene mainMenuScene = new Scene(mainMenuParent);
Stage mainMenuStage = new Stage();
mainMenuStage.setScene(mainMenuScene);
mainMenuStage.setTitle("Main");
mainMenuStage.setResizable(false);
mainMenuStage.centerOnScreen();
mainMenuStage.getIcons().add(new Image("/images/image.png"));
logIn.close();
mainMenuStage.show();
}
FromUsers.java
public class FromUsers
{
private String userName;
public FromUsers(String userName)
{
this.userName = userName;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
}
FxmlControllerMain.java
public void setChosenUser(FromUsers chosenUser)
{
fxLblUser.setText(chosenUser.getUserName());
this.chosenUser = chosenUser;
}
感谢。