2017-09-20 95 views
0

我有问题,我的应用程序不断变化的场景看起来像的JavaFX抛出:IllegalArgumentException(已被设为另一个场景根)

Main screen > Login screen 

我存储在主文件屏幕为hashmap<String, Node>,一切都很好,直到我去从登陆界面到主屏幕回来,想再次加载登录界面,这里是例外,代码:

java.lang.IllegalArgumentException: [email protected][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); 
} 

它,当我创建工程新anchorpane

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

但我想知道为什么它给了我一个异常,如果我正在同台

回答

1

例外的情况是不言自明:锚窗格不能是两个不同的场景的根源。不是每次都创建一个新的场景,只需更换现有场景的根:

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

真的感谢这就是解决我的问题 –

+0

@JasonBourne,你应该纪念这个问题的正确答案。 – Kerry

相关问题