将 JavaFX FXML 对象组合在一起 [英] Grouping together JavaFX FXML Objects

查看:25
本文介绍了将 JavaFX FXML 对象组合在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特别描述性和信息丰富的答案将获得价值 50 点声望的赏金.

我正在 JavaFX 中开发应用程序,对于视图,我使用 FXML.

I am developing an application in JavaFX, and for views, I use FXML.

<AnchorPane id="AnchorPane" fx:id="dashboard" prefHeight="400.0" prefWidth="600.0" stylesheets="@css/dashboard.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.hassanalthaf.telemart.viewmodels.DashboardViewModel">
   <children>
      <MenuBar maxWidth="600.0" minWidth="600.0" prefWidth="600.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
      <AnchorPane fx:id="home" layoutY="29.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="371.0" prefWidth="600.0" />
      <AnchorPane fx:id="about" layoutY="29.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="371.0" prefWidth="600.0" />
      <AnchorPane fx:id="users" layoutY="29.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="371.0" prefWidth="600.0" />
   </children>
</AnchorPane>

如您所见,此代码段包含一些 ,其 ID 为 homeaboutusers.这些是我的应用程序的单独页面.要操作这些窗格,我必须像这样将它们注入到我的代码中:

As you can see, this snippet contains some <AnchorPane>s with the IDs home, about, users. These are separate pages of my application. To manipulate those Panes, I have to inject them to my code like this:

@FXML
private AnchorPane home;

@FXML
private AnchorPane about;

@FXML
private AnchorPane users;

这个现在看起来很整洁,但是当超过20页时,它可能看起来有点凌乱和乏味.有没有办法以一种干净有效的方式将它们组合成一个数组或其他东西?

This may look neat for now, but when there is more than 20 pages, it may look a bit messy and tedious. Is there any way to group them into an array or something in a clean and efficient way?

推荐答案

你可以使用 fx:definefx:reference 将元素放置在 List 和场景图中,并将列表注入到控制器:

You could use fx:define and fx:reference to place the elements in a List and in the scene graph and inject the list to the controller:

<AnchorPane id="AnchorPane" fx:id="dashboard" prefHeight="400.0" prefWidth="600.0" stylesheets="@css/dashboard.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.hassanalthaf.telemart.viewmodels.DashboardViewModel">
   <fx:define>
        <!-- create panes and store them in a list -->
        <ArrayList fx:id="panes">
            <AnchorPane fx:id="home" layoutY="29.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="371.0" prefWidth="600.0" />
            <AnchorPane fx:id="about" layoutY="29.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="371.0" prefWidth="600.0" />
            <AnchorPane fx:id="users" layoutY="29.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="371.0" prefWidth="600.0" />
        </ArrayList>
    </fx:define>
    <children>
        <MenuBar maxWidth="600.0" minWidth="600.0" prefWidth="600.0">
            <menus>
                <Menu mnemonicParsing="false" text="File">
                    <items>
                        <MenuItem mnemonicParsing="false" text="Close" />
                    </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                    <items>
                        <MenuItem mnemonicParsing="false" text="About" />
                    </items>
                </Menu>
            </menus>
        </MenuBar>
        <!-- add panes in the list to scene graph -->
        <fx:reference source="home"/>
        <fx:reference source="about"/>
        <fx:reference source="users"/>
    </children>
</AnchorPane>

控制器

@FXML
private List<AnchorPane> panes;

这篇关于将 JavaFX FXML 对象组合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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