JavaFx将其他FXML加载到FXML“模板”中

我需要创建许多不同的FXML文件,并且每个文件都具有一致的布局。每个都将具有一个AnchorPane,该AnchorPane将保存单独的内容。

有没有一种方法可以加载“基本” FXML文件,然后加载第二个FXML文件,并将该数据路由到第一个文件中?

例如,FXML#1具有BorderPane。FXML#2具有按钮,texfield,标签等。如何加载#1,然后作为#1的子代加载#2?

法比安

您可以使用该<fx:root>元素来允许您向现有元素中添加一些内容。

您需要一种方法来获取对应该用作根元素的节点的引用,并FXMLLoader在加载第二个fxml时将其传递给您可以例如使用名称空间通过fx:id属性获取该元素

@Override
public void start(Stage primaryStage) throws IOException {
    FXMLLoader outerLoader = new FXMLLoader(getClass().getResource("outer.fxml"));

    Scene scene = new Scene(outerLoader.load());

    URL inner = getClass().getResource("inner1.fxml");
    // URL inner = getClass().getResource("inner2.fxml");

    FXMLLoader innerLoader = new FXMLLoader(inner);

    // get insertion point from outer fxml
    innerLoader.setRoot(outerLoader.getNamespace().get("insertionPoint"));

    innerLoader.load();

    primaryStage.setScene(scene);
    primaryStage.show();
}

external.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.*?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <BorderPane AnchorPane.topAnchor="10"  fx:id="insertionPoint"/>
    </children>
</AnchorPane>

inner1.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<fx:root type="javafx.scene.layout.BorderPane" xmlns:fx="http://javafx.com/fxml/1">
    <center>
        <Label text="Hello from inner 1."/>
    </center>
</fx:root>

inner2.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<fx:root type="javafx.scene.layout.BorderPane" xmlns:fx="http://javafx.com/fxml/1">
    <center>
        <Label text="Greetings from inner 2."/>
    </center>
</fx:root>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

卡在javafx程序中,该程序不会将可注入字段加载到FXML文档中

Javascript无法将其他页面加载到DIV中

Javafx将新的fxml文件加载到滚动窗格中

JavaFX8-从其他线程引用FXML对象

加载 FXML 文件的 JavaFX 异常

何时其他类使用php的类中的常数变量将其加载到RAM中?

jQuery:将其他页面中的div加载到div中

将其他数据加载到仍在商店中的EmberData模型中

JavaFX不会像在fxml中那样加载css

如何从数组中过滤多个选定值并将其从uibutton加载到其他uitableview

将其他网页的内容加载到div中并添加样式

尝试将FXML文件加载到定制的JavaFX Game类(由教授制作)

加载 fxml 时出现“javafx.fxml.LoadException”

FXML文档拒绝导入其他fxml文件

绘制线条JavaFX中与FXML

JavaFX:FXML和Item模板的集合绑定

JavaFX使用FXML加载性能问题

JavaFX FXML:加载后为空白WebView

不能重定向到其他FXML

将FXML加载到AnchorPane中时如何自动调整大小?

从配置文件将 MenuItem 加速器加载到 FXML 中?

我可以动态地将其他Spring配置文件加载到现有的WebApplicationContext中吗?

将FXML加载到AnchorPane中会破坏布局吗?

Odoo 11.0仅在渲染模板时才将CSS /脚本或其他内容加载到<head>中

Django:将带有表单的 alluath 模板加载到 div 加载其他内容

Java和JavaFX中的MVC分离(fxml)

SceneBuilder(FXML)中的JavaFX调用默认方法

JavaFx / FXML中的“节点”是什么?

在FXML中设置JavaFX TableView的占位符