我正在做一个盒子,用来记录我正在创建的游戏中发生的事件。为此,我在ScrollPane中放入了一个VBox。这个VBox会被GridPanes填满,目前只包含一个Text。
最初的几个条目都能正常工作,但是当它们达到盒子的最大高度时,它们就开始拥挤起来。我希望高度能随着条目的增长而增长,同时条目不会被挤占。
给VBox一个巨大的最小高度解决了这个问题,但使滚动条太小了,我希望每次添加新条目时,盒子都能滚动到底部。
<AnchorPane prefHeight="144.0" prefWidth="430.0" style="-fx-background-color: #a1aaa0;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.catan.Controller.LogController">
<children>
<ScrollPane id="logScrollPane" fx:id="scrollPane" hbarPolicy="NEVER" prefHeight="144.0" prefWidth="430.0" stylesheets="@style.css">
<content>
<VBox fx:id="logsBox" prefWidth="430.0" prefHeight="144.0" />
</content>
</ScrollPane>
</children>
</AnchorPane>
GridPane的JavaFX代码
private GridPane logGrid = new GridPane();
private Text eventText = new Text();
private HBox imgBox = new HBox();
public void initLogPane(){
logGrid.prefHeight(25);
logGrid.prefWidth(430);
RowConstraints row = new RowConstraints();
row.setPrefHeight(30);
row.setMinHeight(10);
logGrid.getRowConstraints().add(row);
ColumnConstraints column1 = new ColumnConstraints();
column1.setMinWidth(10);
column1.setPrefWidth(Region.USE_COMPUTED_SIZE);
ColumnConstraints column2 = new ColumnConstraints();
column2.setMinWidth(10);
column2.setPrefWidth(84);
logGrid.getColumnConstraints().addAll(column1, column2);
eventText.setWrappingWidth(0);
eventText.setFont(Font.font(13));
logGrid.setMargin(eventText, new Insets(0, 15, 0, 0));
imgBox.prefHeight(100);
imgBox.setSpacing(2);
logGrid.add(eventText, 0, 0);
logGrid.add(imgBox, 1, 0);
}
我看了一下GridPane的RowConstraint,它的minHeight为10。增加这个数字使GridPanes保持了我想要的样子。谢谢Alex