1,java有提供了五种布局管理器:流式布局管理器(FlowLayout)、边界布局管理器(BorderLayout)、网格布局管理器(GridLayout)、卡片布局管理器(CardLayout)、网格包布局管理器(GridBagLayout)。其中前三种是最常见的布局管理器。
2,流式布局是默认的,常用的写法A流式布局:this.setLayout(new FlowLayout(FlowLayout.LEFT)); 单个加上时用此语句 this.add(mb2,FlowLayout.LEFT);流式布局三种写法,a,居中对齐this.setLayout(new FlowLayout()); //由于java默认的是流式布局管理器,添加布局管理器,以免添加出现错误,,b左对齐
//this.setLayout(new FlowLayout(FlowLayout.LEFT));//若无此语句则只显示最后一句c,右对齐
//this.setLayout(new FlowLayout(FlowLayout.RIGHT));
B边界布局:this.add(mb2,BorderLayout.SOUTH); C网格布局:this.setLayout(new GridLayout(3,3,30,3));//前两个数字表示3行*3列,后两个第一个表示左右间距,后一个表示上下间距;后两个可以省略,如 this.setLayout(new GridLayout(3,1));
3,步骤A,先引用awt,swing库,B继承JFrme,定义按钮,C,主函数,类名 **=new 类名 (一般类名和主函数名一致)
D,类的编程,a 先定义按钮名字,JButton关键字 b,添加按钮名字 关键字add c,用何布局以及框格放的位置
4,难点:a,若按钮较多采用循环的方法,
JButton[] an={null,null,null,null,null,null,null,null};//此语句在主函数中定义
for(int i=0;i<s;i++)//s是按钮的个数
{
this.add(an[i]);
}
b,this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭后台JVM,否则每运行一次不关闭导致电脑死机
以下为流式布局举例
import java.awt.*;
import javax.swing.*;
import javax.swing.*;
public class L54 extends JFrame
{
JButton[] an={null,null,null,null,null,null,null,null};
public static void main(String[] args)
{
L54 lx=new L54();
}
public L54()
{
an[0]=new JButton("话梅");
an[1]=new JButton("果浦");
an[2]=new JButton("薯片");
an[3]=new JButton("饼干");
an[4]=new JButton("巧克力");
an[5]=new JButton("腰果");
an[6]=new JButton("锅巴");
an[7]=new JButton("开心果");
//this.setLayout(new FlowLayout()); //由于java默认的是边界布局管理器,添加布局管理器,以免添加出现错误。
this.setLayout(new FlowLayout(FlowLayout.LEFT));
//this.setLayout(new FlowLayout(FlowLayout.RIGHT));
/*this.add(an[0]);
this.add(an[1]);
this.add(an[2]);
this.add(an[3]);
this.add(an[4]);
this.add(an[5]);
this.add(an[6]);
this.add(an[7]);*/
for(int i=0;i<8;i++)
{
this.add(an[i]);
}
this.setTitle("流式布局FlowLayout");
this.setSize(380,120);
this.setLocation(200,200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
流式布局
import java.awt.*;
import javax.swing.*;
import javax.swing.*;
public class L54 extends JFrame
{
JButton[] an={null,null,null,null,null,null,null,null};
public static void main(String[] args)
{
L54 lx=new L54();
}
public L54()
{
an[0]=new JButton("话梅");
an[1]=new JButton("果浦");
an[2]=new JButton("薯片");
an[3]=new JButton("饼干");
an[4]=new JButton("巧克力");
an[5]=new JButton("腰果");
an[6]=new JButton("锅巴");
an[7]=new JButton("开心果");
//this.setLayout(new FlowLayout()); //由于java默认的是边界布局管理器,添加布局管理器,以免添加出现错误。
this.setLayout(new FlowLayout(FlowLayout.LEFT));//若无此语句则只显示最后一句
//this.setLayout(new FlowLayout(FlowLayout.RIGHT));
/*this.add(an[0]);
this.add(an[1]);
this.add(an[2]);
this.add(an[3]);
this.add(an[4]);
this.add(an[5]);
this.add(an[6]);
this.add(an[7]);*/
for(int i=0;i<8;i++)
{
this.add(an[i]);
}
this.setTitle("流式布局FlowLayout");
this.setSize(380,120);
this.setLocation(200,200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
/*总结 1. 继承JFrame类
2. 在最上方定义组件
3. 在构造方法中创建组件
4. 在构造方法中添加组件
5. 设置窗体属性
6. 显示窗体
7. 在主函数中创建对象
8. 所有布局管理器都可以添加任意组件,我这里只是拿按钮举例子,并不是只能添加按钮*/