首页 > 解决方案 > ScrollPane 中的 JavaFX FlowPane,动态调整滚动窗格的内容和大小

问题描述

我目前有一个以 FlowPane 作为内容的 ScrollPane。FlowPane 当前初始化时没有子节点、固定宽度和首选项/最小高度(但没有最大高度)。

在运行时向 FlowPane 添加项目时(我单击一些 UI 元素并将某些内容添加到 FlowPane),ScrollPane 应调整其高度,以防对 FlowPane 的添加不再适合。

我不明白如何设置 flowPane 和 ScrollPane 的高度以使其正常工作 - 如果这是一开始的问题。目前,只要添加到 FlowPane 的内容不适合其初始高度,就会添加内容,但不可见。属于 ScrollPane 的滚动条从不调整它的高度 - 如果是这样,我可以进一步向下滚动并查看内容。

假设我有一个具有一些宽度和高度的 ScrollPane、一些视口宽度/高度和一个具有一些宽度/高度的 FlowPane - 我的最小/首选/最大尺寸应该是什么设置?如何使 scrollPane 调整其滚动条行为或使内容可见?

ScrollPane 的 setFitToHeight 已经设置为 true,这似乎没有改变任何东西。

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class FlowPaneTest extends Application
{

    public static void main(String[] args)
    {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception
    {

        // borderPane rootPane
        BorderPane borderPane = new BorderPane();
        borderPane.setMinSize(600, 600);
        borderPane.setPrefSize(600, 600);
        borderPane.setMaxSize(600, 600);
        // container for the two scrollPanes below
        FlowPane flow = new FlowPane();
        borderPane.setRight(flow);

        // two scrollPanes, each should resize it's height (width should be fixed) if
        // children are added beyond it's current height
        ScrollPane top = new ScrollPane();
        ScrollPane bottom = new ScrollPane();

        FlowPane scrollPaneContent = new FlowPane();
        top.setContent(scrollPaneContent);
        bottom.setContent(scrollPaneContent);

        flow.getChildren().add(top);
        flow.getChildren().add(bottom);

        borderPane.setOnMouseClicked(new EventHandler<Event>()
        {

            @Override
            public void handle(Event event)
            {

                Label l = new Label("test");
                l.setMinSize(100, 100);
                l.setPrefSize(100, 100);
                l.setMaxSize(100, 100);

                scrollPaneContent.getChildren().add(l);

            }
        });

        // size settings

        int width = 300, height = 300;

        top.setHvalue(0.5);

        top.setMinViewportHeight(height);
        top.setPrefViewportHeight(height);
        top.setMinViewportWidth(width);
        top.setPrefViewportWidth(width);
        top.setHbarPolicy(ScrollBarPolicy.ALWAYS);
        top.setFitToHeight(true);

        top.setMinSize(width, height);
        top.setPrefSize(width, height);
        top.setMaxWidth(width);

        scrollPaneContent.setMinSize(width, height);
        scrollPaneContent.setPrefSize(width, height);
        scrollPaneContent.setMaxWidth(width);
        scrollPaneContent.setPrefHeight(height);

        bottom.setMinSize(width, height);
        bottom.setPrefSize(width, height);
        bottom.setMaxWidth(width);
        bottom.setHvalue(0.5);

        bottom.setMinViewportHeight(height);
        bottom.setPrefViewportHeight(height);
        bottom.setMinViewportWidth(width);
        bottom.setPrefViewportWidth(width);
        bottom.setHbarPolicy(ScrollBarPolicy.ALWAYS);
        top.setFitToHeight(true);
        bottom.setFitToHeight(true);

        // stage
        Scene scene = new Scene(borderPane, 600.0, 600.0);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

标签: javajavafx

解决方案


尝试给出ScrollPane首选项高度和宽度并添加此行

scrollPane.setFitToWidth(true);

推荐阅读