* 网格布局
* GridLayout
* 图形界面
* 对象数组 new new
*/
package com.test1;
//package com.GUI;
import java.awt.*;
import javax.swing.*;
public class Demo8_4 extends JFrame//继承JFrame类
{
int size=8; //变量,定义按钮个数
JButton jbs[]=new JButton[size]; //定义声明按钮数组(new)
public static void main(String[] args) //主函数
{
Demo8_4 demo8_4=new Demo8_4();
}
public Demo8_4()//构造函数
{
for (int i=0;i{
jbs[i]=new JButton(String.valueOf(i));//将int i 转变成字符串,创建按钮数组(new2)
}
//设置网格布局
this.setLayout(new GridLayout(3,3,10,10));//设置对象布局(网格布局(行,列,行间距,列间距))
for (int i=0;i{
this.add(jbs[i]); //为对象实例添加组件
}
//设置窗体属性
this.setTitle("网格布局属性");//窗口标题
this.setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setLocation(200, 200);//设置窗口位置
this.setSize(200, 300);//设置窗口大小
this.setVisible(true);//显示窗口