Одно из окон моего приложения GUI состоит из аналогичных HBox с теми же элементами управления внутри. Я хотел сосредоточить все на панели верхнего слоя (в данном случае - Anchor Pane).Настройка привязок JavaFX Anchor Pane в CSS
Вот код:
<?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 есть нижний/левый/правый/верхние анкеры. Я хотел каким-то параметризировать их, установить свойства HBox по умолчанию в моем файле FXML с помощью CSS, например. Но когда я начал искать дополнительную информацию (учебники, ссылки Oracle, Stack), я не нашел ничего полезного.
Может ли кто-нибудь сказать мне, есть ли какой-либо способ, который мне не нужно будет редактировать каждый компонент, например, выровнять его так, как я хочу? Кроме того, он должен соответствовать шаблону проектирования MVC.
У 'HBox' есть' VBox' как их родитель: нет смысла устанавливать для них свойства AnchorPane. –
@James_D, в FXML, если есть Ящики в Якорной панели, можно добавить к ним свои свойства. Они больше настроек выравнивания, чем свойства прямой панели. – notmyf4ulty
Но они не являются (непосредственно) в 'AnchorPane'. Они находятся в 'VBox'. –