FXML Doccument拒绝导入其他fxml文件

时间:2016-03-29 13:37:42

标签: javafx fxml

我的程序的主要FXML文档包含TabPane。对于每个选项卡,我希望它有自己的控制器和fxml文件。当我尝试将外部fmxl文件包含到主fxml文档中时,我的程序拒绝运行。这是我的主要FXML文档: 这是我的java文件的副本

@Override
public void start(Stage stage) throws Exception {
    FXMLLoader fxml = new FXMLLoader();
    Parent root = fxml.load(getClass().getResource("FXMLDocument.fxml").openStream());

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    FXMLDocumentController fdc = fxml.getController();
}

错误:

Caused by: javafx.fxml.LoadException: Base location is undefined. unknown path:97

1 个答案:

答案 0 :(得分:2)

导致此错误的原因是您尚未设置location的{​​{1}}属性,而是指定从中加载FXML的FXMLLoader。我认为InputStream必须知道原始fxml文件的位置才能解析所包含文件的位置。在特殊情况下,您应该只使用FXMLLoader方法:当您从资源(即应用程序jar文件中的文件或资源)之外的源加载fxml时。

相反,请使用

load(InputStream)

或等同地

FXMLLoader fxml = new FXMLLoader();
fxml.setLocation(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxml.load();