javafx IllegalArgumentException(已设置为另一个场景的根) [英] javafx IllegalArgumentException (is already set as root of another scene)

查看:313
本文介绍了javafx IllegalArgumentException(已设置为另一个场景的根)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更改应用程序中的场景时遇到问题

I have problem with changing scenes in my application which looks like

Main screen > Login screen

我将屏幕存储为hashmap<String, Node>在主文件中,一切都很好,直到我从登录屏幕返回主屏幕并想再次加载登录屏幕,这是异常和代码:

I am storing screens in main file as hashmap<String, Node> and everything is good until I go back from login screen to main screen and want to load the login screen again, here is exception and code:

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

当我创建新的锚定窗格时可以使用

It works when I'm creating new anchorpane

Parent root = new AnchorPane(MyApplication.pages.get("LoginPage"));

但是我想了解为什么如果我在同一舞台上工作会给我一个例外

But I want to understand why it gives me an exception if I'm working on the same stage

推荐答案

该异常是不言而喻的:锚窗格不能是两个不同场景的根.不必每次都创建一个新场景,只需替换现有场景的根:

The exception is pretty self-explanatory: the anchor pane cannot be the root of two different scenes. Instead of creating a new scene every time, just replace the root of the existing scene:

@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(已设置为另一个场景的根)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆