在java中创建一个Cell Grid?

时间:2015-01-09 15:42:04

标签: 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);
    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.
}

1 个答案:

答案 0 :(得分:0)

像Tom建议的那样使用JTable,或者使用GridLayout:

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

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