布局管理器
- 流式布局--FlowLayout
1 package com.gui.lesson1; 2 3 import java.awt.*; 4 5 public class TestFlowLayout { 6 public static void main(String[] args) { 7 Frame frame = new Frame(); 8 9 //组件-按钮 10 Button button1 = new Button("button1"); 11 Button button2 = new Button("button2"); 12 Button button3 = new Button("button3"); 13 14 //设置为流式布局 15 frame.setLayout(new FlowLayout(FlowLayout.LEFT)); 16 17 frame.setSize(200, 200); 18 19 //把按钮添加上去 20 frame.add(button1); 21 frame.add(button2); 22 frame.add(button3); 23 24 frame.setVisible(true); 25 } 26 }
- 东西南北中--BorderLayout
1 package com.gui.lesson1; 2 3 import java.awt.*; 4 5 public class TestBorderLayout { 6 public static void main(String[] args) { 7 Frame frame = new Frame("BorderLayout"); 8 9 Button east = new Button("East"); 10 Button west = new Button("West"); 11 Button south = new Button("South"); 12 Button north = new Button("North"); 13 Button center = new Button("Center"); 14 15 //把按钮添加到对应的位置 16 frame.add(east, BorderLayout.EAST); 17 frame.add(west, BorderLayout.WEST); 18 frame.add(south, BorderLayout.SOUTH); 19 frame.add(north, BorderLayout.NORTH); 20 frame.add(center, BorderLayout.CENTER); 21 22 frame.setSize(200, 200); 23 frame.setVisible(true); 24 } 25 }
- 表格布局--GridLayout
1 package com.gui.lesson1; 2 3 import java.awt.*; 4 5 public class TestGridLayout { 6 public static void main(String[] args) { 7 Frame frame = new Frame("GridLayout"); 8 9 Button btn1 = new Button("btn1"); 10 Button btn2 = new Button("btn2"); 11 Button btn3 = new Button("btn3"); 12 Button btn4 = new Button("btn4"); 13 Button btn5 = new Button("btn5"); 14 Button btn6 = new Button("btn6"); 15 16 frame.setLayout(new GridLayout(3, 2)); 17 18 frame.add(btn1); 19 frame.add(btn2); 20 frame.add(btn3); 21 frame.add(btn4); 22 frame.add(btn5); 23 frame.add(btn6); 24 25 //Java函数!会将布局自动的选一个最优秀的位置来确定,相当于自动布局 26 frame.pack(); 27 frame.setVisible(true); 28 } 29 }