在我的FX项目中,我的资源包中有一个名为“fxml”的文件夹,我的资源包中有一个名为“css”的文件夹。我有几个FXML文件使用“global-styles”css文件没有问题,但我的“customer-report-view”fxml文件找不到资源。
FXML目录
CSS目录
我以完全相同的方式链接了css文件
(top.fxml)
<AnchorPane id="anchorPaneTop" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="175.0" prefWidth="996.0" styleClass="header" stylesheets="@../css/global-styles.css" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.tbonefuel.fx.Launcher">
(用户报告-view.fxml)
<AnchorPane fx:id="screenID" prefHeight="474.0" prefWidth="997.0" stylesheets="@../css/global-styles.css" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.tbonefuel.fx.controller.CustomerReportController">
初始化customer-report-view.fxml文件时,我在控制台中收到的消息。 (唯一没有应用样式的文件)
null/../css/global-styles.css
这是用于加载FXML文件和模板的所有代码
public TemplateController show(String fxml) {
try {
TemplateController template = (TemplateController) replaceSceneContent("master-template.fxml", fxml);
template.setApplication(this);
return template;
} catch (Exception ex) {
logger.log(Level.SEVERE, null, ex);
}
return null;
}
/**
* This method sets up application to render screen
* @param _template fxml template file
* @param _displayPage contents of this page will be embed inside the template
* @return instance of display Page Controller
* @throws Exception
*/
private Initializable replaceSceneContent(String _template, String _displayPage) throws Exception {
logger.info("Initializing...");
Pane main;
AnchorPane templatePage;
Initializable mainController;
// get main page
Page mainPage = Page.getPage(_displayPage);
main = mainPage.getPane();
// get reference of Pane to be added to the template.
main = (AnchorPane) main.lookup("#screenID");
mainController = mainPage.getController();
// get template
templatePage = (AnchorPane) getTemplate(_template);
// get reference of the "main" section in template i.e. where the new contents will be added
AnchorPane origMain = (AnchorPane) templatePage.lookup("#main");
// set contents of "mainPage" into the template
origMain.getChildren().setAll(main.getChildren());
// create scene
Scene scene = new Scene(templatePage, 1024, 768);
//set stage
LauncherContext.getContext().getStage().setScene(scene);
LauncherContext.getContext().getStage().sizeToScene();
return mainController;
}
/**
* load template
* @param _template template file name
* @return Pane
* @throws IOException
*/
private AnchorPane getTemplate(String _template) throws IOException {
AnchorPane templatePage;
FXMLLoader loader = new FXMLLoader();
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(Launcher.class.getResource("/fxml/" + _template));
templatePage = (AnchorPane) loader.load();
return templatePage;
}