首页 > 解决方案 > 如何创建一个永远在垂直方向滚动的滚动窗格?

问题描述

我不确定这是否可行,我在上面找不到任何东西,但我可以永远做一个滚动窗格滚动吗?一旦按下按钮,我就会不断地将新按钮和标签添加到窗口中,并最终到达底部。我可能设置错误,但这里是代码:

BorderPane bp = new BorderPane();
GridPane gp = new GridPane();
Button b = new Button("click");
gp.add(b, 1, 1);
ScrollPane sp = new ScrollPane(gp);
bp.setTop(sp);
b.setOnAction(e -> createLabel());

标签: javajavafx-8scrollbarscrollpanegridpane

解决方案


您快到了,您现在需要做的就是将滚动窗格添加为容器的子项

        GridPane gp = new GridPane();
        Button b = new Button("click");
        gp.add(b, 1, 1);
        b.setOnAction(e -> createLabel());

        ScrollPane sp = new ScrollPane(gp);

        container.add(sp); // where container is whatever node that'll contain the gridpane.

玩这个代码

public class Controller {
    @FXML private VBox topLevelContainer; // root fxml element

    @FXML
    void initialize(){

        GridPane gridPane = new GridPane();
        ScrollPane sp = new ScrollPane(gridPane);
        topLevelContainer.getChildren().add(sp);

        // add a 100 buttons to 0th column
        for (int i = 0; i < 100; i++) {
            gridPane.add(new Button("button"),0,i);
        }


    }

}

推荐阅读