使用布局管理器
  FlowLayout管理器
  面板的默认布局管理器是java.awt包中的FlowLayout类。使用FlowLayout时,像在页面中排列英文单词那样排组件:从左到右排列,当前行没有空间后进入下一行。
1 import java.awt.*;
2 import javax.swing.*;
3
4 public class Crisis extends JFrame {
5     JButton panicButton;
6     JButton dontPanicButton;
7     JButton blameButton;
8     JButton mediaButton;
9     JButton saveButton;
10
11     public Crisis() {
12         super("Crisis");
13         setLookAndFeel();
14         setSize(348, 128);
15         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
16 //        1.默认布局
17         FlowLayout flo = new FlowLayout();
18         setLayout(flo);
19         panicButton = new JButton("Panic");
20         dontPanicButton = new JButton("Don't Panic");
21         blameButton = new JButton("Blame Others");
22         mediaButton = new JButton("Notify the Media");
23         saveButton  = new JButton("save yourself");
24         add(panicButton);
25         add(dontPanicButton);
26         add(blameButton);
27         add(mediaButton);
28         add(saveButton);
29         setVisible(true);
30     }
31
32     private void setLookAndFeel() {
33         try {
34             UIManager.setLookAndFeel(
35                     "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
36         } catch (Exception exc) {
37             // ignore error
38         }
39     }
40
41     public static void main(String[] arguments) {
42         Crisis frame = new Crisis();
43     }
44 }
  GridLayout管理器
  GridLayout类位于java.awt包中,它将容器中所有的组件组织为指定的行数和列数。分配给每个组件的显示区域都相同。
  当组件加入到容器中时,GridLayout将所有的组件放置到网格中的某个位置,而且组件是从左到右依次添加,当这一行满了之后,在从下一行的左边开始添加。
1 import java.awt.*;
2 import javax.swing.*;
3
4 public class Crisis extends JFrame {
5     JButton panicButton;
6     JButton dontPanicButton;
7     JButton blameButton;
8     JButton mediaButton;
9     JButton saveButton;
10
11     public Crisis() {
12         super("Crisis");
13         setLookAndFeel();
14         setSize(348, 128);
15         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
16
17 //        2.GridLayout布局
18         GridLayout grid = new GridLayout(2, 3);
19         setLayout(grid);
20         panicButton = new JButton("Panic");
21         dontPanicButton = new JButton("Don't Panic");
22         blameButton = new JButton("Blame Others");
23         mediaButton = new JButton("Notify the Media");
24         saveButton  = new JButton("save yourself");
25         add(panicButton);
26         add(dontPanicButton);
27         add(blameButton);
28         add(mediaButton);
29         add(saveButton);
30         setVisible(true);
31     }
32
33     private void setLookAndFeel() {
34         try {
35             UIManager.setLookAndFeel(
36                     "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
37         } catch (Exception exc) {
38             // ignore error
39         }
40     }
41
42     public static void main(String[] arguments) {
43         Crisis frame = new Crisis();
44     }
45 }
  BorderLay管理器
  BorderLayout类也位于java.awt包中,它将容器中的组件放置在特定的位置,该位置有5个方位:东、西、南、北、中。
  BorderLayout管理器将组件放置到5个位置:其中4个位置由罗盘方向指定,另外一个由中心区域指定。当在该布局下添加组件时,add()方法会包含第2个参数,用于指示组件应该放置的位置。该参数应该是BorderLayout类的5个类变量之一:NORTH、SOUTH、EAST、WEST和CENTER。
  与GridLayout类相同,BorderLayout也会将所有可用空间都分配给组件。在周围放置4个边界组件后,余下的空间都分配给中央的组件,因此它通常是大的。
1 import java.awt.*;
2 import javax.swing.*;
3
4 public class Crisis extends JFrame {
5     JButton panicButton;
6     JButton dontPanicButton;
7     JButton blameButton;
8     JButton mediaButton;
9     JButton saveButton;
10
11     public Crisis() {
12         super("Crisis");
13         setLookAndFeel();
14         setSize(348, 128);
15         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
16         BorderLayout crisisLayout = new BorderLayout();
17         setLayout(crisisLayout);
18
19         panicButton = new JButton("Panic");
20         dontPanicButton = new JButton("Don't Panic");
21         blameButton = new JButton("Blame Others");
22         mediaButton = new JButton("Notify the Media");
23         saveButton  = new JButton("save yourself");
24         add(panicButton, BorderLayout.NORTH);
25         add(dontPanicButton, BorderLayout.SOUTH);
26         add(blameButton, BorderLayout.EAST);
27         add(mediaButton, BorderLayout.WEST);
28         add(saveButton, BorderLayout.CENTER);
29         setVisible(true);
30     }
31
32     private void setLookAndFeel() {
33         try {
34             UIManager.setLookAndFeel(
35                     "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
36         } catch (Exception exc) {
37             // ignore error
38         }
39     }
40
41     public static void main(String[] arguments) {
42         Crisis frame = new Crisis();
43     }
44 }