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

发布于 2024-08-26 14:09:21 字数 105 浏览 6 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚你发端 2024-09-02 14:09:21

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

类似于:

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]);
   }
}

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

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

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"));
ζ澈沫 2024-09-02 14:09:21

    import java.awt.GridLayout //Grid layout from awt docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html

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

创建面板并设置其布局。
new GridLayout(行数、列数、水平间隙、垂直间隙)

(新的GridLayout(2,2,1,1)) =>这里我想要2行,2列,
- 如果有任何水平间隙(HGap),它们应该是1px(1unit)
- 我也想要相同的垂直间隙,所以我做与垂直间隙(VGap)相同的事情。即1个单位

- 在这种情况下;差距=>间距/边距/填充——从这个意义上说。

创建组件并将其添加到面板

- (分量, 0,0 ) => 0,0 是行和列..(就像二维数组)。 @行 0 & @column 0 或位于第 0 行和第 0 列的交叉点

通过将行和列放在应该放置的位置来指定组件的放置位置。
每个单元格都有一个位置 == [行][列]

或者您可以在没有 hgaps 和 vgaps 的情况下完成:

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

Yes

    import java.awt.GridLayout //Grid layout from awt docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html

    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,VerticalGap)

(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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文