Java 使用 GridLayout 时,我可以将组件添加到特定网格单元格吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2510159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:38:34  来源:igfitidea点击:

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

javauser-interfaceswinglayoutjpanel

提问by Roman

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?

当我将 GridLayout 设置为 JPanel 然后添加一些内容时,它会按“文本顺序”(从左到右,从上到下)随后添加。但我想将一个元素添加到特定单元格(在第 j 列的第 i 行中)。是否可以?

采纳答案by Rob Heiser

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.

不,您不能在特定单元格添加组件。您可以做的是添加空的 JPanel 对象并在数组中保留对它们的引用,然后以您想要的任何顺序向它们添加组件。

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:

然后,您可以直接添加到 JPanel 对象之一:

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

回答by TimeTrax

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.

创建您的面板并设置其布局。
new GridLayout(numberOfRows,numberOfColums,Horizo​​ntalGap,VerticleGap)

(new GridLayout(2,2,1,1)) => 这里我想要 2 行,2 列,
--如果有水平间隙 (HGap)​​,它们应该是 1px ( 1unit)
-- 我也希望垂直间隙也一样,所以我和垂直间隙 (VGAp) 一样。即 1 个单位
——在这种情况下;间隙 => 间距/边距/填充——从这个意义上说。

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]

创建您的组件并将它们添加到面板
- (component, 0,0 ) => 0,0 是行和列..(就像一个二维数组)。@row 0 & @column 0 或在第 0 行和第 0 列的交叉点
通过将行和列放在它应该去的地方来指定你的组件去哪里。
每个单元格都有一个位置 == [行][列]

Or you can do it without hgaps and vgaps:

或者你可以在没有 hgaps 和 vgaps 的情况下做到这一点:

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