【问题标题】:How to align an HRow inside a GridPane?如何在 GridPane 中对齐 Hrow?
【发布时间】:2016-05-14 03:34:12
【问题描述】:

我在运行 JavaFx 应用程序时遇到了这种奇怪的行为:

如您所见,元素未正确对齐。我已经使用 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>

我错过了什么吗?

【问题讨论】:

    标签: javafx


    【解决方案1】:

    GridPane 中控件的默认对齐方式(也许是一般的控件?)是CENTER_LEFT,而HBox 的默认对齐方式是TOP_LEFT。添加

    alignment="CENTER_LEFT"
    

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

    【讨论】:

      猜你喜欢
      • 2019-12-29
      • 2019-01-08
      • 2016-07-31
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      • 2014-11-09
      • 2016-05-28
      相关资源
      最近更新 更多