我使用此代码将 FlowPane 放入 ScrollPane 中
FlowPane flowPane = new FlowPane(Orientation.HORIZONTAL);
ScrollPane scroll = new ScrollPane();
scroll.setContent(flowPane);
scroll.viewportBoundsProperty().addListener(new ChangeListener<Bounds>()
{
@Override
public void changed(ObservableValue<? extends Bounds> ov, Bounds oldBounds, Bounds bounds)
{
flowPane.setPrefWidth(bounds.getWidth());
flowPane.setPrefHeight(bounds.getHeight());
}
});
不幸的是,这可能不是最好的解决方案。在 Java 8 中还有另一种更简单的方法将 FlowPane 放入 ScrollPane 中吗?
ScrollPane 有一些方法可以做到这一点:
scrollPane.setFitToWidth(true);
scrollPane.setFitToHeight(true);
尝试通过CSS或API将flowPane的
maxWidth
和maxHeoght
属性设置为Double.MAX_VALUE
,以便在容器内提供最大的扩展:
flowPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);