非常具有描述性和信息性的答案将从我这里获得价值 50 声望的赏金。
我正在 JavaFX 中开发一个应用程序,对于视图,我使用 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>
如您所见,此片段包含一些<AnchorPane>
s 与 IDhome
, about
, users
。这些是我的申请的单独页面。要操作这些窗格,我必须将它们注入到我的代码中,如下所示:
@FXML
private AnchorPane home;
@FXML
private AnchorPane about;
@FXML
private AnchorPane users;
现在看起来可能很整洁,但是当超过 20 页时,它可能看起来有点混乱和乏味。有没有什么方法可以以干净有效的方式将它们分组到数组或其他东西中?
你可以使用fx:define https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#define_elements and fx:reference https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#reference_elements将元素放入List
并在场景图中将列表注入到控制器中:
<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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)