如何将 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
答案 0 :(得分:4)
您需要修改第一个 RowConstraints 并将 valignment 属性设置为 VPos.TOP
。
行的垂直对齐方式。如果设置,将是默认值 行中包含的节点的垂直对齐方式。如果这 属性设置为 VPos.BASELINE,则 fillHeight 属性将是 忽略,节点将始终调整到其首选高度 (docs)。
像这样:
[...]
<rowConstraints>
<RowConstraints valignment="TOP" vgrow="SOMETIMES" /> <!-- added valignment="TOP" -->
<RowConstraints vgrow="SOMETIMES" />
<RowConstraints />
<RowConstraints />
</rowConstraints>
[...]