我的程序有一个主要的 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();
}
Run Code Online (Sandbox Code Playgroud)
错误:
Caused by: javafx.fxml.LoadException: Base location is undefined. unknown path:97
Run Code Online (Sandbox Code Playgroud)
导致此错误的原因是您尚未设置 的location属性FXMLLoader,而是指定了InputStream从中加载 FXML 的 。我认为FXMLLoader必须需要知道原始 fxml 文件的位置才能解析包含文件的位置。您真的应该只load(InputStream)在特殊情况下使用该方法:当您从资源以外的源(即应用程序 jar 文件中的文件或资源)加载 fxml 时。
相反,使用
FXMLLoader fxml = new FXMLLoader();
fxml.setLocation(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxml.load();
Run Code Online (Sandbox Code Playgroud)
或者,等效地,
FXMLLoader fxml = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxml.load();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1467 次 |
| 最近记录: |