FXML Doccument拒绝导入其他fxml文件 [英] FXML Doccument refusing to import other fxml files

查看:322
本文介绍了FXML Doccument拒绝导入其他fxml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a main FXML document for my program which contains a TabPane. For each tab I want it to have its own controller and fxml file. When I try to include the external fmxl files into the main fxml document, my program refuses to run. here is my main FXML document: here is a copy of my java file

@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


推荐答案

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

This error is caused because you have not set the location property of the FXMLLoader, and instead you are specifying an InputStream from which to load the FXML. I think the FXMLLoader must need to know the location of the original fxml file in order to resolve the location of the included file. You should really only use the load(InputStream) method in exceptional circumstances: when you are loading the fxml from a source other than a resource (i.e. file or resource in your application jar file).

相反,使用

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();

这篇关于FXML Doccument拒绝导入其他fxml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆