javafx 中的可滚动网格窗格

Scrollable gridpane in javafx

我正在尝试将乘法 VBox 添加到滚动窗格内的网格窗格(在以下代码片段中称为 refPane)。

    int columnIndex = 0;
    int rowIndex = 0;
    int boxWidth = windowWidth/ITEMS_PER_ROW;
    int boxHeight = windowHeight/ITEMS_PER_COLUMN;

    for(int i=0; i<items.size(); i++){
        VBox vBox = new VBox();
        vBox.setPrefWidth(boxWidth);
        vBox.setPrefHeight(boxHeight);

        Label label1 = new Label();
        label1.setText("ImgPlaceholder");
        label1.setPrefWidth(boxWidth);
        label1.setPrefHeight(boxHeight / 100 * 70);
        vBox.getChildren().add(label1);

        Label label2 = new Label();
        label2.setText("Description");
        label2.setPrefWidth(boxWidth);
        label2.setPrefHeight(boxHeight / 100 * 30);
        label2.setPadding(new Insets(0,0,0, 10));
        vBox.getChildren().add(label2);

        refPane.add(vBox, columnIndex, rowIndex);

        if(columnIndex != 0 && columnIndex % GAMES_PER_ROW == 0){
            rowIndex++;
            columnIndex = 0;
        }else {
            columnIndex++;
        }

它在一行中添加不超过 ITEMS_PER_ROW 个 Vbox,并在下一行中继续。此外,不应该再有 ITEMS_PER_COLUM 可见的行。 问题是,如果我向网格添加更多然后 ITEMS_PER_ROW * ITEMS_PER_COLUMN,而不是 ob 可滚动,则 vbox 的尺寸会变小。

有什么想法吗?提前致谢。

很可能 javafx 优先考虑缩小 VBox 而不是扩展网格窗格。尝试将每个 VBox 的 minHeight 设置为等于其 prefHeight 以防止它们垂直收缩。