import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridLayOutTest
{
public static void main(String[] args)
{
GridLayOutFrame frame = new GridLayOutFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class GridLayOutFrame extends JFrame
{
public GridLayOutFrame()
{
setTitle("网格布局管理器");
setSize(300, 200);
Container con = getContentPane();
GridLayOutPanel panel = new GridLayOutPanel();
con.add(panel);
}
}
class GridLayOutPanel extends JPanel
{
public GridLayOutPanel()
{
//实例化网格布局管理器
GridLayout layout = new GridLayout(3,3,10,10);
//设置布局管理器
setLayout(layout);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
}
}