Can I add a component to a specific grid cell when a GridLayout is used?

江枫思渺然 提交于 2019-12-17 10:53:23

问题


When I set the GridLayout to the JPanel and then add something, it is added subsequently in the "text order" (from left to right, from top to bottom). But I want to add an element to a specific cell (in the i-th row in the j-th column). Is it possible?


回答1:


No, you can't add components at a specific cell. What you can do is add empty JPanel objects and hold on to references to them in an array, then add components to them in any order you want.

Something like:

int i = 3;
int j = 4;
JPanel[][] panelHolder = new JPanel[i][j];    
setLayout(new GridLayout(i,j));

for(int m = 0; m < i; m++) {
   for(int n = 0; n < j; n++) {
      panelHolder[m][n] = new JPanel();
      add(panelHolder[m][n]);
   }
}

Then later, you can add directly to one of the JPanel objects:

panelHolder[2][3].add(new JButton("Foo"));



回答2:


Yes

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(2,2,1,1));
    JButton component= new JButton("Component");
    panel.add(component, 0,0 );

Create your panel and set its layout.
new GridLayout(numberOfRows,numberOfColums,HorizontalGap,VerticleGap)

(new GridLayout(2,2,1,1))  => here i want 2 rows, 2 columns,
-- if any horizontal gaps (HGap), they should be 1px (1unit)
-- I also want the same for vertical gaps so i do same as vertical gaps(VGap). i.e 1 unit
-- In this case; gaps  => spacing/margins/padding --in that sense.

Create your components and add them to the panel
-- (component, 0,0 )  => 0,0 is the row and column.. (like a 2d array). @row 0 & @column 0 or at intersection of row 0 and column 0
specify where your component goes by putting the row and column where it should go.
each cell has a location == [row][column]

Or you can do it without hgaps and vgaps:

    JPanel panel = new JPanel();        
    panel.setLayout(new GridLayout(2,2));
    JButton component= new JButton("Component");
    panel.add(component, 0,0 );


来源:https://stackoverflow.com/questions/2510159/can-i-add-a-component-to-a-specific-grid-cell-when-a-gridlayout-is-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!