JComponents 的 2D 网格 (Swing)
我正在尝试制作 JTextFields 的 2D 网格来表示级别图。我已经完成以下初始化 TextFields 的 2D 数组的操作:
fields = new TextField[level.rows][level.columns];
TextField field;
for (int r = 0; r < level.rows; r++) {
for (int c = 0; c < level.columns; c++) {
field = new TextField(level.bricks[r][c].type);
fields[r][c] = field;
}
现在我必须将它们添加到 JFrame 中,但它们需要对齐,以便每个行
都位于另一列下方。我确实找到了 GridLayout,但是我对 AWT/Swing 不太有经验,并且仍然不知道如何实现所需的布局。我期待有某种类似 gLayout.add(JComponent,row,column) 的方法。
I'm trying to make a 2D grid of JTextFields to represent a level map. I've come to the following for initializing the 2D Array of TextFields:
fields = new TextField[level.rows][level.columns];
TextField field;
for (int r = 0; r < level.rows; r++) {
for (int c = 0; c < level.columns; c++) {
field = new TextField(level.bricks[r][c].type);
fields[r][c] = field;
}
Now I have to add them to the JFrame, but they need to be lined up so that every row
gets under the other column. I did find GridLayout, however i'm not very experienced with AWT/Swing, and still don't know how achieve the desired layout. I was expecting there to be some kind of method like gLayout.add(JComponent,row,column)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该像您所解释的那样工作,这是一个完整的工作示例,将
JLabels
放入 5x5 网格中:最终结果如下所示:
希望这会有所帮助。
This should work as you explained, here's a fully working example that puts
JLabels
in the 5x5 Grid:The end result would look something like this:
Hope this helps.
您应该小心添加顺序,容器是在 GridLayout 中一列又一列地填充的。
You should be careful about adding order, container is filled column after column in GridLayout.