使用 GridLayout 时可以将组件添加到特定网格单元吗?
当我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可以,您不能在特定单元格添加组件。您可以做的是添加空的 JPanel 对象并在数组中保留对它们的引用,然后按您想要的任何顺序向它们添加组件。
类似于:
然后,您可以直接添加到 JPanel 对象之一:
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:
Then later, you can add directly to one of the JPanel objects:
是
创建面板并设置其布局。
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 的情况下完成:
Yes
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: