三种布局形式


1.行列式布局(GridLayout):(即按给定的行和列来进行布局)

 1 public static void main(String[] args)
 2     {
 3         Frame frame = new Frame("TestGridLayout");
 4         
 5         Button btn1 = new Button("btn1");
 6         Button btn2 = new Button("btn2");
 7         Button btn3 = new Button("btn3");
 8         Button btn4 = new Button("btn4");
 9         Button btn5 = new Button("btn5");
10         Button btn6 = new Button("btn6");
11         
12         frame.setLayout(new GridLayout(3,2));
13         
14         frame.add(btn1);
15         frame.add(btn2);
16         frame.add(btn3);
17         frame.add(btn4);
18         frame.add(btn5);
19         frame.add(btn6);
20         
21         frame.pack();//自动布局
22         frame.setVisible(true);
23     }

实际效果:

2.流式布局(FlawLayout):

 1 public static void main(String[] args)
 2     {
 3         Frame frame = new Frame();
 4         //组件-按钮
 5         Button button1 = new Button("button1");
 6         Button button2 = new Button("button2");
 7         Button button3 = new Button("button3");
 8         //设置为流式布局
 9         frame.setLayout(new FlowLayout(FlowLayout.LEFT));//从左到右流式布局
10         //添加按钮
11         frame.add(button1);
12         frame.add(button2);
13         frame.add(button3);
14         
15         frame.setVisible(true);
16     }

3.方位布局,分为东南西北中五个方位(BorderLayout):

 1 public static void main(String[] args)
 2     {
 3         Frame frame = new Frame("TestBorderLayout");
 4         Button east = new Button("East");
 5         Button south = new Button("South");
 6         Button west = new Button("West");
 7         Button north = new Button("North");
 8         Button center = new Button("Center");
 9         
10         frame.add(east,BorderLayout.EAST);
11         frame.add(west,BorderLayout.WEST);
12         frame.add(north,BorderLayout.NORTH);
13         frame.add(south,BorderLayout.SOUTH);
14         frame.add(center,BorderLayout.CENTER);
15         
16         frame.setSize(200,200);
17         frame.setVisible(true);
18     }

相关