问题
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();
}
Error:
Caused by: javafx.fxml.LoadException: Base location is undefined. unknown path:97
回答1:
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).
Instead, use
FXMLLoader fxml = new FXMLLoader();
fxml.setLocation(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxml.load();
or, equivalently,
FXMLLoader fxml = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxml.load();
来源:https://stackoverflow.com/questions/36285996/fxml-doccument-refusing-to-import-other-fxml-files