在java中创建单元网格?

在java中创建单元网格?,java,swing,user-interface,Java,Swing,User Interface,这是我的GUI代码,我正在尝试创建一个单元格网格,但我很难做到这一点。有人能帮忙吗 JButton Game = null; JButton Quit = null; Container ctr = null; public GUI() { JFrame frame = new JFrame(); frame.setSize(500,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FlowL

这是我的GUI代码,我正在尝试创建一个单元格网格,但我很难做到这一点。有人能帮忙吗

JButton Game = null;
JButton Quit = null;
Container ctr = null;

public GUI()
{
    JFrame frame = new JFrame();
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout fl = new FlowLayout();
    ctr = frame.getContentPane();
    ctr.setLayout(fl);
使用容器创建框架

    Game = new JButton("Start Game");
    Game.setToolTipText("Press button to start Game Of Life");
    ctr.add(Game);
    Game.setBackground(Color.WHITE);
    Game.addActionListener(this);
    //Creating a button to start the game 

    Quit = new JButton("Exit");
    Quit.setToolTipText("Click to exit program");
    ctr.add(Quit);
    Quit.setBackground(Color.WHITE);
    Quit.addActionListener(new CloseListener());
    //Creating a button to quit the game when clicked

    frame.setVisible(true);
    frame.setResizable(false);
    frame.setBackground(Color.WHITE);
    Game.setBounds(150,420,100,40);
    Quit.setBounds(250,420,100,40);
    //button.setBounds(x, y, width, height);

    These are the frame settings which are not final yet.
}

使用Tom建议的JTable,或使用GridLayout:

ctr.setLayout(new GridLayout(3, 0)); // 3 buttons across, X buttons down

每个单元格的宽度/高度相等,但使用起来比JTable简单得多。

您可以查看以下内容:。请阅读我的文章,了解创建Swing grid的一种方法。我已经尝试过这样做,但界面上没有出现任何问题。