javafx IllegalArgumentException(已设置为另一个场景的根)
javafx IllegalArgumentException (is already set as root of another scene)
我在应用程序中更改场景时遇到问题,看起来像
Main screen > Login screen
我将屏幕作为 hashmap<String, Node>
存储在主文件中,一切都很好,直到我从登录屏幕返回到主屏幕并想再次加载登录屏幕,这里是异常和代码:
java.lang.IllegalArgumentException: AnchorPane@30561c33[styleClass=root]is already set as root of another scene
public static final HashMap<String, Parent> pages = new HashMap<>();
@FXML
private void LogIn(ActionEvent event) {
Button button = (Button) event.getSource();
Stage stage = (Stage) button.getScene().getWindow();
if(stage.getScene() != null) {stage.setScene(null);}
Parent root = MyApplication.pages.get("LoginPage");
Scene scene = new Scene(root, button.getScene().getWidth(), button.getScene().getHeight());
stage.setScene(scene);
}
它在我创建新锚定窗格时有效
Parent root = new AnchorPane(MyApplication.pages.get("LoginPage"));
但我想明白为什么我在同一个舞台上工作时会给我一个例外
异常是不言自明的:锚定窗格不能是两个不同场景的根。不用每次都创建一个新的场景,只需替换现有场景的根即可:
@FXML
private void LogIn(ActionEvent event) {
Button button = (Button) event.getSource();
Scene scene = button.getScene();
Parent root = MyApplication.pages.get("LoginPage");
scene.setRoot(root);
}
我在应用程序中更改场景时遇到问题,看起来像
Main screen > Login screen
我将屏幕作为 hashmap<String, Node>
存储在主文件中,一切都很好,直到我从登录屏幕返回到主屏幕并想再次加载登录屏幕,这里是异常和代码:
java.lang.IllegalArgumentException: AnchorPane@30561c33[styleClass=root]is already set as root of another scene
public static final HashMap<String, Parent> pages = new HashMap<>();
@FXML
private void LogIn(ActionEvent event) {
Button button = (Button) event.getSource();
Stage stage = (Stage) button.getScene().getWindow();
if(stage.getScene() != null) {stage.setScene(null);}
Parent root = MyApplication.pages.get("LoginPage");
Scene scene = new Scene(root, button.getScene().getWidth(), button.getScene().getHeight());
stage.setScene(scene);
}
它在我创建新锚定窗格时有效
Parent root = new AnchorPane(MyApplication.pages.get("LoginPage"));
但我想明白为什么我在同一个舞台上工作时会给我一个例外
异常是不言自明的:锚定窗格不能是两个不同场景的根。不用每次都创建一个新的场景,只需替换现有场景的根即可:
@FXML
private void LogIn(ActionEvent event) {
Button button = (Button) event.getSource();
Scene scene = button.getScene();
Parent root = MyApplication.pages.get("LoginPage");
scene.setRoot(root);
}