gpt4 book ai didi

javafx IllegalArgumentException(已设置为另一个场景的根)

转载 作者:行者123 更新时间:2023-12-01 23:12:36 27 4
gpt4 key购买 nike

我在更改应用程序中的场景时遇到问题,看起来像

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

当我创建新的 anchor 板时它有效
Parent root = new AnchorPane(MyApplication.pages.get("LoginPage"));

但我想明白为什么如果我在同一个舞台上工作,它会给我一个异常(exception)

最佳答案

异常(exception)是不言自明的: anchor Pane 不能是两个不同场景的根。而不是每次都创建一个新场景,只需替换现有场景的根:

@FXML
private void LogIn(ActionEvent event) {
Button button = (Button) event.getSource();
Scene scene = button.getScene();
Parent root = MyApplication.pages.get("LoginPage");
scene.setRoot(root);
}

关于javafx IllegalArgumentException(已设置为另一个场景的根),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46328192/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com