GridPane:如何将标签定位在左上角?
GridPane: How to position a Label top-left?
如何将 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
需要将第一个RowConstraints and set the valignment属性修改为VPos.TOP
.
The vertical alignment for the row. If set, will be the default
vertical alignment for nodes contained within the row. If this
property is set to VPos.BASELINE, then the fillHeight property will be
ignored and nodes will always be resized to their preferred heights (docs).
像这样:
[...]
<rowConstraints>
<RowConstraints valignment="TOP" vgrow="SOMETIMES" /> <!-- added valignment="TOP" -->
<RowConstraints vgrow="SOMETIMES" />
<RowConstraints />
<RowConstraints />
</rowConstraints>
[...]
如何将 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
需要将第一个RowConstraints and set the valignment属性修改为VPos.TOP
.
The vertical alignment for the row. If set, will be the default vertical alignment for nodes contained within the row. If this property is set to VPos.BASELINE, then the fillHeight property will be ignored and nodes will always be resized to their preferred heights (docs).
像这样:
[...]
<rowConstraints>
<RowConstraints valignment="TOP" vgrow="SOMETIMES" /> <!-- added valignment="TOP" -->
<RowConstraints vgrow="SOMETIMES" />
<RowConstraints />
<RowConstraints />
</rowConstraints>
[...]