如何在Scenebuilder中添加一个Grid,其中包含更多单元格的特定列,然后是列的其余部分?

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

我想添加一个网格布局,每行有不同的单元格。至于现在,我只能添加或删除行或列,我想询问是否可以在特定行或列中添加单元格。

例如,在下面的网格图像中,我想问一下如何在第2列中添加单元格。我的意思是我想在第2列中添加比列enter image description here的其余部分更多的单元格

javafx scenebuilder
1个回答
0
投票

您可以将每列设为1列GridPane。每列可以包含任意数量的行。将所有这些列放在另一个1行GridPane中:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class FxTest extends Application {

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

        GridPane  grid = new GridPane();
        grid.setPadding(new Insets(5));
        for (int i= 0; i< 5 ; i++){
            GridPane column = makeColumn(3+i);
            grid.add(column, i, 0);
        }

        Scene scene = new Scene(new Group(grid));
        primaryStage.setScene(scene);
        primaryStage.sizeToScene();
        primaryStage.show();
    }

    private GridPane makeColumn(int columns) {
        GridPane column = new GridPane();
        column.setPadding(new Insets(2));
        column.setGridLinesVisible(true);
        for(int i = 0; i < columns; i++){
            column.add(new Label("  "+i +"  "), 0, i);
        }
        return column;
    }

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

enter image description here

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