如何创建一个永远在垂直方向滚动的滚动窗格?

问题描述 投票:1回答:1

我不确定这是否可行而且我找不到任何东西,但是我可以永远制作滚动条卷轴吗?按下按钮后,我不断在窗口中添加新的按钮和标签,最终到达底部。我可能设置错了,但这里是代码:

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());
java javafx-8 scrollbar scrollpane gridpane
1个回答
0
投票

你几乎就在那里,你现在需要做的就是将滚动窗格添加为容器所在的孩子

        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);
        }


    }

}
© www.soinside.com 2019 - 2024. All rights reserved.