java之在 CSS 中设置 JavaFX Anchor Pane 的 anchor
我的 GUI 应用程序中的一个窗口由内部具有相同控件的类似 HBox 组成。我想将所有内容都放在顶层 Pane 中(在本例中为 anchor Pane )。
代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Button?>
<AnchorPane xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
<VBox fx:id="modeWindowPane" alignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<HBox alignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label contentDisplay="CENTER" maxHeight="1.7976931348623157E308" prefWidth="100.0" text="Current" />
<TextField prefWidth="100.0" />
<Button maxHeight="1.7976931348623157E308" mnemonicParsing="false" text="Set Current"/>
</children>
</HBox>
<HBox alignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label contentDisplay="CENTER" maxHeight="1.7976931348623157E308" prefWidth="100.0" text="Frequency" />
<TextField prefWidth="100.0" />
<Button maxHeight="1.7976931348623157E308" mnemonicParsing="false" text="Set Current"/>
</children>
</HBox>
<HBox alignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label contentDisplay="CENTER" maxHeight="1.7976931348623157E308" prefWidth="100.0" text="Fade In" />
<TextField prefWidth="100.0" />
<Button maxHeight="1.7976931348623157E308" mnemonicParsing="false" text="Set Current"/>
</children>
</HBox>
<HBox alignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label contentDisplay="CENTER" maxHeight="1.7976931348623157E308" prefWidth="100.0" text="Fade Out" />
<TextField prefWidth="100.0" />
<Button maxHeight="1.7976931348623157E308" mnemonicParsing="false" text="Set Current"/>
</children>
</HBox>
<HBox alignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label contentDisplay="CENTER" maxHeight="1.7976931348623157E308" prefWidth="100.0" text="Offset" />
<TextField prefWidth="100.0" />
<Button maxHeight="1.7976931348623157E308" mnemonicParsing="false" text="Set Current"/>
</children>
</HBox>
<HBox alignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<Label contentDisplay="CENTER" maxHeight="1.7976931348623157E308" prefWidth="100.0" text="Stimulation Time" />
<TextField prefWidth="100.0" />
<Button maxHeight="1.7976931348623157E308" mnemonicParsing="false" text="Set Current"/>
</HBox>
</VBox>
</AnchorPane>
你可以看到在每个 HBox 中都有 bottom/left/right/top anchor 。我想以某种方式对它们进行参数化,例如通过 CSS 在我的 FXML 文件中设置默认 HBox 属性。但是当我开始寻找更多信息(教程、Oracle 的引用资料、堆栈)时,我没有找到任何有用的信息。
有人能告诉我是否有任何方法可以让我不必编辑每个组件,例如,以我想要的方式对齐它?此外,它必须符合 MVC 设计模式。
请您参考如下方法:
看看 JavaFX CSS Reference Guide .在那里你可以找到所有 CSS 属性的完整文档。据我所知,您不能通过 CSS 为对象设置 anchor 。
您可以通过 Controller 实现。只需给所有元素一个 fx:id
并在您的 Controller 中分配它们。然后您可以迭代它们并设置 anchor (例如 AnchorPane.setTopAnchor(Node, Value)
)。有关详细信息,请参阅 AnchorPane .
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。