Java GUI编程(5)- 3种布局管理器

流式布局

 1 public class TestFlowLayout {
 2     public static void main(String[] args) {
 3         Frame frame = new Frame();
 4 
 5         //组件-按钮
 6         Button button1 = new Button("button01");
 7         Button button2 = new Button("button02");
 8         Button button3 = new Button("button03");
 9 
10         //设置成流式布局
11         frame.setLayout(new FlowLayout());//居中
12         frame.setLayout(new FlowLayout(FlowLayout.LEFT));//居左
13         frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//居右
14 
15         frame.setSize(200,200);
16         frame.add(button1);
17         frame.add(button2);
18         frame.add(button3);
19 
20         frame.setVisible(true);
21 
22         frame.addWindowListener(new WindowAdapter() {
23             @Override
24             public void windowClosing(WindowEvent e) {
25                 System.exit(0);
26             }
27         });
28     }
29 }

东西南北中布局

 1 //东西南北中布局
 2 public class TestBorderLayout {
 3     public static void main(String[] args) {
 4         Frame frame = new Frame();
 5 
 6         Button east = new Button("east");
 7         Button west = new Button("west");
 8         Button south = new Button("south");
 9         Button north = new Button("north");
10         Button centor = new Button("centor");
11 
12         frame.setSize(400,400);
13 
14         frame.add(east,BorderLayout.EAST); //东
15         frame.add(west,BorderLayout.WEST); //西
16         frame.add(south,BorderLayout.SOUTH);//南
17         frame.add(north,BorderLayout.NORTH);//北
18         frame.add(centor,BorderLayout.CENTER);//中
19 
20         frame.setVisible(true);
21 
22         frame.addWindowListener(new WindowAdapter() {
23             @Override
24             public void windowClosing(WindowEvent e) {
25                 System.exit(0);
26             }
27         });
28     }
29 }

表格式布局

 1 //表格式布局
 2 public class TestGridLayout {
 3     public static void main(String[] args) {
 4         Frame frame = new Frame();
 5 
 6         Button button1 = new Button("button1");
 7         Button button2 = new Button("button2");
 8         Button button3 = new Button("button3");
 9         Button button4 = new Button("button4");
10         Button button5 = new Button("button5");
11         Button button6 = new Button("button6");
12 
13         frame.setSize(500,500);
14 
15         frame.setLayout(new GridLayout(3,2)); //三行两列
16 
17         frame.add(button1);
18         frame.add(button2);
19         frame.add(button3);
20         frame.add(button4);
21         frame.add(button5);
22         frame.add(button6);
23 
24         frame.setVisible(true);
25 
26         frame.addWindowListener(new WindowAdapter() {
27             @Override
28             public void windowClosing(WindowEvent e) {
29                 System.exit(0);
30             }
31         });
32     }
33 }

练习

Java GUI编程(5)- 3种布局管理器

 1 public class TestHomeWork {
 2     public static void main(String[] args) {
 3 
 4         //总frame,一分为二,上下两部分
 5         Frame frame = new Frame();
 6         frame.setSize(500,400);
 7         frame.setVisible(true);
 8         frame.setLayout(new GridLayout(2,1));
 9 
10         //上下两个panel面板
11         Panel panel1 = new Panel();
12         panel1.setBackground(new Color(246, 142, 175));
13         panel1.setLayout(new BorderLayout());
14         Panel panel2 = new Panel(new BorderLayout());
15         panel2.setBackground(new Color(71, 196, 214));
16 
17         frame.add(panel1);
18         frame.add(panel2);
19 
20         //上面的面板
21         panel1.add(new Button("01-east"),BorderLayout.EAST);
22         panel1.add(new Button("01-west"),BorderLayout.WEST);
23 
24         Panel panel3 = new Panel();
25         panel3.setLayout(new GridLayout(2,1));
26         panel3.add(new Button("01-01"));
27         panel3.add(new Button("01-02"));
28         panel1.add(panel3,BorderLayout.CENTER);
29 
30         //下面的面板
31         panel2.add(new Button("02-east"),BorderLayout.EAST);
32         panel2.add(new Button("02-west"),BorderLayout.WEST);
33 
34         Panel panel4 = new Panel(new GridLayout(2,2));
35         for (int i = 0; i <4 ; i++) {
36             panel4.add(new Button("02-"+i));
37         }
38         
39         panel2.add(panel4);
40         
41         frame.addWindowListener(new WindowAdapter() {
42             @Override
43             public void windowClosing(WindowEvent e) {
44                 System.exit(0);
45             }
46         });
47     }
48 }

总结

  1. frame是一个*窗口
  2. Panel无法单独显示,必须添加到某个容器种
  3. 布局管理器
    1. 流式
    2. 东西南北中
    3. 表格
  4. 大小、定位、背景颜色、可见性
上一篇:布局练习


下一篇:wpf(四)