public class TestFlowLayout
{
public static void main(String[] args)
{
Frame f = new Frame("FlowLayout");
Button button1 = new Button("Ok");
Button button2 = new Button("Open");
Button button3 = new Button("Close");
f.setLayout(new FlowLayout(FlowLayout.CENTER));//f默认布局管理器不是FlowLayout,中心位置
f.add(button1);
f.add(button2);
f.add(button3);
f.setSize(300,400);
f.setVisible(true);
}
}
--------------------------------------------------------------------------------
/*BorderLayout布局管理器是Frame类的默认管理器*/
import java.awt.*;
public class TestFlowLayout2
{
public static void main(String[] args)
{
Frame f = new Frame("Java Frame");
FlowLayout l = new FlowLayout(FlowLayout.CENTER,30,30);//水平和垂直距离都是30,并且无论怎么调整窗口都是居中对齐
f.setLayout(l);
f.setLocation(300,400);
f.setSize(300,100);//如果尺寸太小,按钮会显示不全
f.setBackground(new Color(255,222,255));
for(int i = 1; i<7; i++)
{
f.add(new Button("BUTTON"));
}
f.setVisible(true);
}
}
-----------------------------------------------------------------------------------
/*BorderLayout布局管理器是Frame类的默认管理器*/
//添加组件如不声明默认添加到Center并会覆盖全窗口
import java.awt.*;
public class TestBorderLayout
{
public static void main(String[] args)
{
Frame f;
f = new Frame("Border Layout");
Button bn = new Button("BN");
Button bs = new Button("BS");
Button bw = new Button("BW");
Button be = new Button("BE");
Button bc = new Button("BC");
f.add(bn,"North");
f.add(bs,"South");
f.add(bw,"West");
f.add(be,"East");
f.add(bc,"Center");
f.setSize(300,300);
f.setVisible(true);
/*最好这样写:
f.add(bn,BorderLayout.NORTH);
f.add(bs,BorderLayout.SOUTH);
f.add(bw,BorderLayout.WEST);
f.add(be,BorderLayout.EAST);
f.add(bc,BorderLayout.CENTER);
*/
}
}
-----------------------------------------------------------------------------
/*GirdLayout的构造函数
public GridLayout(int,int)
*/
import java.awt.*;
public class TestGridLayout
{
public static void main(String[] args)
{
Frame f = new Frame ("GirdLayout Example");
Button b1 = new Button("b1");
Button b2 = new Button("b2");
Button b3 = new Button("b3");
Button b4 = new Button("b4");
Button b5 = new Button("b5");
f.setLayout(new GridLayout(2,10));//控制几行几列
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.pack();//如果把该语句注释掉,则输出结果变成只有一个标题栏的小窗口,自动识别默认组件大小
f.setVisible(true);
}
}