JComponents 的 2D 网格 (Swing)

发布于 2025-01-07 13:47:00 字数 543 浏览 5 评论 0原文

我正在尝试制作 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 技术交流群。

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

发布评论

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

评论(2

晨曦÷微暖 2025-01-14 13:47:00

这应该像您所解释的那样工作,这是一个完整的工作示例,将 JLabels 放入 5x5 网格中:

import java.awt.*;
import javax.swing.*;

public class GridPrb {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container cp = frame.getContentPane();
    cp.setLayout(new GridLayout(5, 5));

    for(int y = 0; y < 5; y++) {
      for(int x = 0; x < 5; x++) {
        Label l = new Label("x=" + x + ",y=" + y);
        cp.add(l);
      }
    }
    frame.pack();
    frame.setVisible(true);
  }
}

最终结果如下所示:

在此处输入图像描述

希望这会有所帮助。

This should work as you explained, here's a fully working example that puts JLabels in the 5x5 Grid:

import java.awt.*;
import javax.swing.*;

public class GridPrb {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container cp = frame.getContentPane();
    cp.setLayout(new GridLayout(5, 5));

    for(int y = 0; y < 5; y++) {
      for(int x = 0; x < 5; x++) {
        Label l = new Label("x=" + x + ",y=" + y);
        cp.add(l);
      }
    }
    frame.pack();
    frame.setVisible(true);
  }
}

The end result would look something like this:

enter image description here

Hope this helps.

小嗲 2025-01-14 13:47:00

您应该小心添加顺序,容器是在 GridLayout 中一列又一列地填充的。

frame.setLayout( new GridLayout( level.rows, level.columns );

for (int c = 0; c < level.columns; r++) 
    for (int r = 0; r < level.rows; c++) 
         frame.add(fields[r][c]);

You should be careful about adding order, container is filled column after column in GridLayout.

frame.setLayout( new GridLayout( level.rows, level.columns );

for (int c = 0; c < level.columns; r++) 
    for (int r = 0; r < level.rows; c++) 
         frame.add(fields[r][c]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文