1.流布局管理器:
FlowLayout布局管理器中组件的相对位置随窗口大小而变化。


下面是流布局演示代码:
package cn.hncu.MyJFrame1;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FlowLayoutJFrame extends JFrame {
public FlowLayoutJFrame(){
JButton Jbtn1,Jbtn2,Jbtn3;
this.setBounds(300, 300, 400, 100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
Jbtn2 = new JButton("bbbbb");
this.add(Jbtn2,"CENTER");
Jbtn3 = new JButton("ccccc");
this.add(Jbtn3,"RIGHT");
Jbtn1 = new JButton("aaaaa");
this.add(Jbtn1,"LEFT");
this.setVisible(true);
}
public static void main(String[] args) {
new FlowLayoutJFrame();
}
}
2.边布局管理器:
BorderLayout,当容器大小改变时,四边组件的长度或者宽度不变,
中间组件的长度和宽度都随容器大小而变化。


下面是边布局管理器的演示代码:
package cn.hncu.MyJFrame1;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class BorderLayoutJFrame extends JFrame{
public BorderLayoutJFrame(){
JButton Jbtn[] =new JButton[5];
this.setBounds(300, 300, 400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new BorderLayout(5,6));
String strJbtns = "北东南西中";
for(int i=0;i<strJbtns.length();i++){
Jbtn[i] = new JButton(""+strJbtns.charAt(i));
}
this.getContentPane().add(Jbtn[0],BorderLayout.NORTH);
this.getContentPane().add(Jbtn[1],BorderLayout.EAST);
this.getContentPane().add(Jbtn[2],BorderLayout.SOUTH);
this.getContentPane().add(Jbtn[3],BorderLayout.WEST);
this.getContentPane().add(Jbtn[4],BorderLayout.CENTER);
this.setVisible(true);
}
public static void main(String[] args) {
new BorderLayoutJFrame();
}
}
3.网格布局管理器:
GridLayout布局管理器将容器划分为大小相等的若干行乘若干列的网格,
组件大小随容器大小而变化。


下面为网格布局演示代码:
package cn.hncu.MyJFrame1;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Label;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridLayoutJFrame extends JFrame{
public GridLayoutJFrame(){
this.setBounds(300, 300, 400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(3,3,10,20));
String strJbtns[] = {"一","二","三","四","五","六","七","八"};
for(int i=0;i<strJbtns.length;i++){
this.add(new JButton(strJbtns[i]));
}
this.add(new Label(""),2);
this.setVisible(true);
}
public static void main(String[] args) {
new GridLayoutJFrame();
}
}