import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGridLayout extends JFrame{
public static void main(String[] args) {
new TestGridLayout();
}
public TestGridLayout(){
GridLayout gl=new GridLayout(3,3,5,10);
/*GridLayout gl=new GridLayout();空构造方法;
*GridLayout gl=new GridLayout(3,3);代表三行四列
*GridLayout gl=new GridLayout(3,3,5,6);代表三行四列,横向间隔为5,竖向间隔是6
* */
this.setLayout(gl);
this.add(new JButton("1按钮"));
this.add(new JButton("2按钮"));
this.add(new JButton("3按钮"));
this.add(new JButton("4按钮"));
this.add(new JButton("5按钮"));
this.add(new JButton("6按钮"));
this.add(new JButton("7按钮"));
this.add(new JButton("8按钮"));
this.add(new JButton("9按钮"));
this.setTitle("GridLayout 网格布局");//设置标题文字
this.setSize(400,300);//设置大小
this.setVisible(true);//设置可见性
this.addWindowListener(new WindowAdapter(){//设置关闭监听事件
public void windowClosing (WindowEvent e){
System.exit(0);
}
});
}
}
/*GridLayout(网格布局):可以使容器中的各组件网格状分布。容器中各组件的高度和宽度相同,当容器的尺寸发生变化时,
* 各组件的相对位置不变,但各自的尺寸会发生变化。
* 各组件的排列方式:从左到右,从上到下。
* 与BorderLayout类似,如果想在一个网格中添加多个组件,则必须在该网格的单元放置一个JPanel,再将多个组件放在面板中。
* */