如何在GridPane中对齐HRow?

时间:2016-02-04 13:34:40

标签: javafx

当我运行JavaFx应用程序时,我有这种奇怪的行为:

screen_capture

正如您所看到的,元素没有正确对齐。我使用SceneBuilder创建了该视图,编辑器中没有这个空白。

我的观点由GridPane组成。 GridPane的右侧单元格(在图片上)每个都包含一个HBox,每个包含元素(按钮/文本字段)。我没有在这里配置任何特定的东西,我没有使用任何CSS。

这里是完整的FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MyController">
   <children>
      <GridPane hgap="3.0" vgap="3.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columnConstraints>
          <ColumnConstraints hgrow="NEVER" />
          <ColumnConstraints hgrow="NEVER" />
            <ColumnConstraints hgrow="ALWAYS" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints vgrow="SOMETIMES" />
          <RowConstraints vgrow="SOMETIMES" />
          <RowConstraints vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label text="label1" />
            <Label text="label2" GridPane.rowIndex="1" />
            <Button defaultButton="true" mnemonicParsing="false" onAction="#handleGo" text="Go!" GridPane.rowIndex="2">
               <graphic>
                  <TextField fx:id="iterations" alignment="CENTER" prefWidth="43.0" style="-fx-padding: 0.166667em 0.333333em 0.166667em 0.333333em;" text="50000" />
               </graphic></Button>
            <Button fx:id="selectFileButton" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#handleFileSelection" text="Select file" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <HBox spacing="3.0" GridPane.columnIndex="2">
               <children>
                  <TextField fx:id="customValue" editable="false" HBox.hgrow="ALWAYS" />
                  <Button fx:id="deleteButton" mnemonicParsing="false" onAction="#handleDelete" text="Delete" visible="false" />
               </children>
            </HBox>
            <HBox spacing="3.0" GridPane.columnIndex="2" GridPane.rowIndex="1">
               <children>
                  <Button fx:id="editButton" mnemonicParsing="false" text="Edit" />
                  <Button mnemonicParsing="false" text="New" />
               </children>
            </HBox>
            <ComboBox fx:id="selection" maxWidth="1.7976931348623157E308" onAction="#handleSelection" GridPane.columnIndex="1" />
         </children>
      </GridPane>
   </children>
</AnchorPane>

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

GridPane中的控件的默认对齐方式(可能一般用于控件?)为CENTER_LEFT,而对于HBox,默认为TOP_LEFT。添加

alignment="CENTER_LEFT"

到您的HBox应正确对齐它们。