Java的GUI组件的布局管理器

 1 import java.awt.BorderLayout;
 2 import java.awt.FlowLayout;
 3 import java.awt.Font;
 4 import java.awt.GridLayout;
 5 import javax.swing.JButton;
 6 import javax.swing.JFrame;
 7 import javax.swing.JLabel;
 8 
 9 public class ShowTest extends JFrame{
10     public ShowTest(){
11         
12     }
13     public static void main(String[] args){
14         ShowTest task = new ShowTest();
15         task.setTitle("TestLayout");
16         task.setSize(300, 300);
17         task.setLocationRelativeTo(null);
18         task.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19         task.setVisible(true);
20         //task.testFlowLayout();
21         //task.testGridLayout();
22         //task.testBorderLayout();
23     }
24     public void testFlowLayout()
25     {
26         //FlowLayout(alignment:int, hap:int, vgap:int)对齐方式,水平间隔,垂直间隔。
27         setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
28         JButton[] jbt = new JButton[9]; 
29         for (int i = 0; i < 9; i++) {
30             jbt[i] = new JButton("" + i);
31             jbt[i].setFont(new Font("SansSerif", Font.BOLD, 20));
32             add(jbt[i]);
33         }
34     }
35     public void testGridLayout(){
36         setLayout(new GridLayout(3, 3));
37         JButton[] jbt = new JButton[8]; 
38         for (int i = 0; i < 8; i++) {
39             jbt[i] = new JButton("" + i);
40             jbt[i].setFont(new Font("SansSerif", Font.BOLD, 20));
41             add(jbt[i]);
42         }
43     }
44     public void testBorderLayout(){
45         //BorderLayout(hgap:int, vgap:int)指定水平间隔和垂直间隔
46         setLayout(new BorderLayout(5, 10));
47         add(new JButton("East"), BorderLayout.EAST);
48         add(new JButton("South"), BorderLayout.SOUTH);
49         add(new JButton("West"), BorderLayout.WEST);
50         add(new JButton("North"), BorderLayout.NORTH);
51         add(new JButton("Center"), BorderLayout.CENTER);
52     }
53 }
task.testFlowLayout();


task.testGridLayout();


task.testBorderLayout();


posted @ 2013-11-26 10:49  soul390  阅读(277)  评论(0编辑  收藏  举报