我了解如何在两个表单之间传递值ala https://www.youtube.com/watch?v=HFAsMWkiLvg
问题在于视频中的完成方式。 (即静态)。由于使用了 getClass() 方法,我无法让 FXMLLoaders 在静态方法内部工作。它只是非静态的。
getClass().getResource("myFile.fxml")
这是我加载第二个表单的方式。
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("LoginForm.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("HWI - Login");
stage.setResizable(false);
stage.setScene(new Scene(root1));
stage.showAndWait();
} catch (Exception e) {
e.printStackTrace();
}
在场景构建器内部,我设置要运行的方法,它本质上是检查数据库中的用户名/密码。所有这些都是在我的 loginController 类中完成的。一旦成功,它就会这样做。 (上面我为 loginButton 声明了 @FXML 钩子(Hook))
Stage stage = (Stage) loginButton.getScene().getWindow();
stage.close();
我当前的程序设置方式是在用户登录之前将所有菜单设置为禁用。我已经设置了一个非静态方法来重新启用所有内容,但我无法调用它因为在关闭第二个窗口之前我无法弥合静态/非静态之间的差距。
谢谢
最佳答案
在 UI 的两个或多个不同部分之间共享数据的普遍接受的方法是使用 MVC/MVP 类型的方法。在这些模式中,您将数据封装在“模型”(其中的“M”)中,以便可以观察数据并在数据发生变化时发送通知。然后 UI 的一部分可以更新模型,如果模型发生变化,观察模型的 UI 的其他部分将会收到通知。
JavaFX 通过实现 observable properties 让这变得特别容易对于您来说,它只是包装值并可以轻松添加监听器,这些监听器会在值发生更改时收到通知。
因此,在这个示例中,您可能会执行以下操作:
public class AuthenticationState {
private final BooleanProperty loggedIn = new SimpleBooleanProperty(false);
public BooleanProperty loggedInProperty() {
return loggedIn ;
}
public final boolean isLoggedIn() {
return loggedInProperty().get();
}
public final void setLoggedIn(boolean loggedIn) {
loggedInProperty().set(loggedIn);
}
private final StringProperty userName = new SimpleStringProperty();
public StringProperty userNameProperty() {
return userName ;
}
public final String getUserName() {
return userNameProperty().get();
}
public final void setUserName(String userName) {
userNameProperty().set(userName);
}
// other properties as needed, e.g. IntegerProperty logInAttempts , etc.
}
现在您的主 Controller 可以执行以下操作:
public class MainController {
@FXML
private final MenuItem deleteAllDataMenuItem ;
private AuthenticationState authenticationState ;
public void initialize() {
authenticationState = new AuthenticationState();
deleteAllDataMenuItem.disableProperty()
.bind(authenticationState.loggedInProperty().not());
}
@FXML
public void logIn() {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("LoginForm.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
LoginController loginController = fxmlLoader.getController();
loginController.setAuthenticationState(authenticationState);
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("HWI - Login");
stage.setResizable(false);
stage.setScene(new Scene(root1));
stage.showAndWait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
您的登录 Controller 可能如下所示:
public class LoginController {
private AuthenticationState authenticationState ;
public void setAuthenticationState(AuthenticationState authenticationState) {
this.authenticationState = authenticationState ;
}
@FXML
public void login() {
// check login:
boolean loginSuccessful = ... ;
authenticationState.setLoggedIn(loginSuccessful);
// ...
}
}
现在,当用户登录时,登录 Controller 会将 authenticationState
中的 loggedIn
属性设置为 true
。由于菜单项的禁用状态绑定(bind)到 loggedIn
属性(其负数),因此菜单项会自动启用。如果您有“注销”按钮,只需将其 loggedIn
属性设置为 false
,菜单项就会再次被禁用。
关于java - 在通过 fxml 设置的两个 JavaFX 窗口之间传递值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41193067/