swing之gridlayout

package gui1;

import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class gridlayout1 extends JFrame{
    JButton [] a={null,null,null,null,null,null,null,null};
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        gridlayout1 g1=new gridlayout1();
    }
public gridlayout1()
         {
    
    a[0]=new JButton("瓜子");
    a[1]=new JButton("话梅");
    a[2]=new JButton("西瓜");
    a[3]=new JButton("草莓");
    a[4]=new JButton("香蕉");
    a[5]=new JButton("苹果");
    a[6]=new JButton("栗子");
    a[7]=new JButton("黄瓜");
    //a[8]=new JButton("葡萄");
    
    this.setLayout(new GridLayout(3,3,15,15));//默认是边界布局,如果想变成其他布局,加上
    
    this.add(a[0]);
    this.add(a[1]);
    this.add(a[2]);
    this.add(a[3]);
    this.add(a[4]);
    this.add(a[5]);
    this.add(a[6]);
    this.add(a[7]);
    
    this.setTitle("网格布局gridlayout");
    this.setSize(400,320);
    this.setLocation(100, 180);
    this.setResizable(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    
    
         }
}

原文地址:https://www.cnblogs.com/luo-mao/p/6030509.html