【问题标题】:GridPane: How to position a Label top-left?GridPane:如何将标签定位在左上角?
【发布时间】:2021-05-13 11:22:51
【问题描述】:

如何将Label Label 1 定位在单元格的左上角?我尝试相应地设置标签的对齐方式,但这并没有成功。

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<GridPane hgap="14.0" vgap="7.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
   <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" />
      <ColumnConstraints hgrow="SOMETIMES" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="-Infinity" prefWidth="400.0" />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints vgrow="SOMETIMES" />
      <RowConstraints vgrow="SOMETIMES" />
      <RowConstraints />
      <RowConstraints />
   </rowConstraints>
   <children>
      <Label text="Label 1" />
      <VBox spacing="4.0" GridPane.columnIndex="2">
         <children>
            <HBox spacing="4.0">
               <children>
                  <TextField styleClass="" HBox.hgrow="ALWAYS">
                     <tooltip>
                        <Tooltip style="-fx-font-size: 100%;" text="Subject 1; Subject 2; Subject 3" />
                     </tooltip>
                  </TextField>
                  <Button mnemonicParsing="false" text="Add" />
               </children>
            </HBox>
            <ScrollPane hbarPolicy="NEVER" maxHeight="-Infinity" prefHeight="61.0" />
         </children>
      </VBox>
      <Label text="Label 2" GridPane.rowIndex="1" />
      <CheckBox mnemonicParsing="false" GridPane.columnIndex="1" GridPane.rowIndex="1" />
      <HBox alignment="CENTER_LEFT" spacing="4.0" GridPane.columnIndex="2" GridPane.rowIndex="1">
         <children>
            <TextField HBox.hgrow="ALWAYS" />
            <Hyperlink text="duplicate" />
         </children>
      </HBox>
   </children>
</GridPane>

d

【问题讨论】:

    标签: java javafx fxml


    【解决方案1】:

    您需要修改第一个RowConstraints并将valignment属性设置为VPos.TOP

    行的垂直对齐方式。如果设置,将是默认值 行中包含的节点的垂直对齐。如果这 属性设置为 VPos.BASELINE,则 fillHeight 属性将为 被忽略,并且节点将始终调整为它们的首选高度 (docs)。

    像这样:

    [...]
    <rowConstraints>
       <RowConstraints valignment="TOP" vgrow="SOMETIMES" /> <!-- added valignment="TOP" -->
       <RowConstraints vgrow="SOMETIMES" />
       <RowConstraints />
       <RowConstraints />
    </rowConstraints>
    [...]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 2016-06-20
      • 1970-01-01
      • 2014-03-02
      相关资源
      最近更新 更多