回答“是否有可能做我想做的事(GridPane inside ScrollPane inside GridPane)”的帖子中间的问题!这是它的 fxml 代码(不要介意常量,我只是将它放在场景构建器中):
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children><ScrollPane prefHeight="200.0" prefWidth="200.0">
<content><GridPane prefHeight="132.0" prefWidth="297.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</content></ScrollPane>
</children>
</GridPane>
制作它的诀窍是让你有一个带有额外行/列的滚动条,而不会使所有现有的行变小,每次动态添加行时调整最里面的网格窗格的高度,这将确保网格窗格的大小滚动窗格,因此使滚动窗格“滚动”
例如(伪代码):
onButtonPress{
resizeGridPane(sizeOfRow+paddingBetweenRows);
AddRowInDesiredLocation(location);
}
我认为这会给你想要的效果。
编辑
在注意到您的 vbox 位于网格窗格内的滚动窗格内之后,您必须适当地调整 vbox 和网格窗格的大小以查看滚动窗格的“滚动”效果,如果您只调整网格窗格的大小,你只会得到连续越来越小的行。