e624. getting the number of rows and columns of cells in a gridbaglayout.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 17 行

TXT
17
字号
The number of rows and columns in a GridBagLayout automatically grows as needed when components are added. The current number of rows and columns can be determined by calling getLayoutDimensions(). However, it won't return the correct values until the components have been laid out at least once before the call to getLayoutDimensions(). Unless you can be sure the components have been laid out at least once, you should explicitly call layoutContainer() to force a layout of the components. 
    GridBagLayout gbl = new GridBagLayout();
    container.setLayout(gbl);
    
    // Add components to container and gbl
    
    // Force the layout of components before calling getLayoutDimensions()
    gbl.layoutContainer(container);
    
    // Get the dimensions
    int[][] dim = gbl.getLayoutDimensions();
    int cols = dim[0].length;
    int rows = dim[1].length;

 Related Examples 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?