(GridLayout)网格布局 :网格的世界更精彩

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,再将多个组件放在面板中。
 * */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值