设置JavaFX锚定窗格';CSS中的s锚

设置JavaFX锚定窗格';CSS中的s锚,java,css,javafx,fxml,Java,Css,Javafx,Fxml,我的GUI应用程序中的一个窗口由内部具有相同控件的类似HBox组成。我想在顶层窗格中居中放置所有内容(在本例中为锚定窗格) 代码如下: <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.control.Label?> <?import javafx.scene.layout.VBox?> &l

我的GUI应用程序中的一个窗口由内部具有相同控件的类似HBox组成。我想在顶层窗格中居中放置所有内容(在本例中为锚定窗格)

代码如下:

<?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中都有底部/左侧/右侧/顶部锚定。我想以某种方式对它们进行参数化,例如通过CSS在FXML文件中设置默认的HBox属性。但当我开始寻找更多信息(教程、Oracle的参考资料、堆栈)时,我没有发现任何有用的信息

有人能告诉我,是否有任何方法我不必编辑每个组件,例如,以我想要的方式对齐它?此外,它还必须符合MVC设计模式。

看看 . 在这里,您可以找到所有CSS属性的完整文档。据我所知,你不能通过CSS为一个对象设置锚点


您可以通过控制器进行控制。只需给所有项目一个
fx:id
a,然后在控制器中分配它们。然后您可以迭代它们并设置锚定(例如,
AnchorPane.settopanch(节点,值)
)。有关更多信息,请参见。

HBoxes有一个
VBox
作为其父项:为它们设置
AnchorPane
属性是没有意义的。@James_D,在FXML中,如果锚窗格中有框,可以将其属性添加到其中。它们更多的是对齐设置,而不是直接窗格的属性。但是它们(直接)不在
AnchorPane
中。它们位于
VBox
中。通常,在JavaFX中,CSS用于定义样式,而不是布局。布局属性应在FXML中设置。但现在还不清楚你想在这里实现什么:可能使用合适的锚定比在这里使用
AnchorPane
要好。@James\u D:我用StackPane代替了AnchorPane。现在一切都按照我的要求进行了。谢谢:)谢谢,但我知道这封推荐信。我看过了,恐怕你说的是真的。我知道这可以通过控制器实现。但我想以MVC设计模式的方式设计我的项目,我不确定是否允许在Controller中编辑布局。