“fxmlLoadException:已指定根值”但我从未设置过根值

问题描述 投票:0回答:1

我正在尝试在单击超链接时打开一个新窗口。出于某种原因,它工作了一次,然后我再也无法让它工作了。每次我运行它我都会得到一个javafx.fxml.LoadException: Root value already specified.错误。问题是我没有在任何地方设置根值。

我试过loader.setRoot(null),但没有做任何事情。我从来没有在我的fxml或常规方法中设置根,所以我不确定它在哪里得到它。

这是加载新窗口的方法

@FXML
private void initialize(){

        messageSupports.setOnMouseClicked(e -> {
            try {
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(getClass().getResource("/MessagingSupportLayout.fxml"));
             //   Parent root = (Parent) loader.load();
                Stage secondaryStage = new Stage();
                loader.setController(new MessagingSupport(mainController));

                mainController.getContentPane().getChildren().add(loader.load());
                secondaryStage.setTitle("Support");
                secondaryStage.setHeight(500);
                secondaryStage.setWidth(350);

                Scene scene = new Scene(loader.load());

                secondaryStage.setScene(scene);
                secondaryStage.show();

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
    }
}

这是MessagingSupportLayout.fxml的fxml。

<BorderPane xmlns="http://javafx.com/javafx"
        prefHeight="400.0" prefWidth="600.0">
</BorderPane>

预期结果将打开一个新窗口。我在start方法中设置了我的主类中的第一个窗口的标题,高度和宽度。我没有在那里或在fxml中声明一个root。如果你需要看,我可以发布它。谢谢您的帮助!

java javafx fxml scenebuilder
1个回答
0
投票

弄清楚了。线mainController.getContentPane().getChildren().add(loader.load());设置根。删除该行可以解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.