1. 流式布局
```java
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
// 组件-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
// 设置为流式布局
// frame.setLayout(new FlowLayout(FlowLayout.LEFT));
// 设置为靠左
// frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
// 设置为靠右
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
// 设置为居中,默认为center
frame.setSize(200,200);
frame.setVisible(true);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
}
}
```

如上图所示依次为设置为,靠左、靠右、居中的效果展示,流式布局是一行一行的。
2. 东南西北中

public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
Button east = new Button("East");
Button west = new Button("West");
Button north = new Button("North");
Button south = new Button("South");
Button center = new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(north,BorderLayout.NORTH);
frame.add(south,BorderLayout.SOUTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(200,200);
frame.setVisible(true);
}
}
3.表格式布局
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
frame.setLayout(new GridLayout(3,2)); // 设置为三行两列
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack(); // JAVA函数!会自动选择最优位置进行布局
frame.setVisible(true);
frame.setSize(200,200);
}
}