我的程序有一个主要的 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();
}
Error:
Caused by: javafx.fxml.LoadException: Base location is undefined. unknown path:97
这个错误是因为你没有设置location
的财产FXMLLoader
,而是您指定一个InputStream
从中加载 FXML。我觉得FXMLLoader
必须需要知道原始 fxml 文件的位置才能解析包含文件的位置。你真的应该只使用load(InputStream)
特殊情况下的方法:当您从资源以外的源(即应用程序 jar 文件中的文件或资源)加载 fxml 时。
相反,使用
FXMLLoader fxml = new FXMLLoader();
fxml.setLocation(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxml.load();
或者,等价地,
FXMLLoader fxml = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxml.load();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)