使用GridBagLayout控制行列的高度和寬度


摘自http://bbs.csdn.net/topics/340189065

使用GridBagLayout控制行列的高度和寬度

 


gridwidth  
指定組件顯示區域的某一行中的單元格數。 默認值1,水平占一格
gridheight  指定在組件顯示區域的一列中的單元格數。默認值1,垂直占一格
weightx  指定如何分布額外的水平空間。 默認值0,額外水平空間不分配。
weighty  指定如何分布額外的垂直空間。 默認值0,額外垂直空間不分配。
ipadx  指定組件最小寬度,可確保組件不會過份縮放。
ipady  指定組件最小高度,可確保組件不會過份縮放。
gridx  指定組件左上角所在位置橫坐標。
gridy  指定組件左上角所在位置縱坐標。
inserts  在組件周圍增加空白區域。
anchor  單個組件水平、垂直都不填充時,設置組件應該顯示在區域的哪個方位,西北還是東南。  也就是說anchor 和fill互斥,fill優先級高



拉伸后效果如下

 
        
 1 package com.hw.gridbaglayout;
 2 
 3 import java.awt.Button;
 4 import java.awt.Font;
 5 import java.awt.GridBagConstraints;
 6 import java.awt.GridBagLayout;
 7 import java.awt.event.WindowAdapter;
 8 import java.awt.event.WindowEvent;
 9 
10 import javax.swing.JFrame;
11 import javax.swing.JPanel;
12 
13 public class GridBagEx2 extends  JPanel
14 {
15     private static final long serialVersionUID = -5214441555967215113L;
16 
17     protected void makebutton(String name, GridBagLayout gridbag,
18             GridBagConstraints c)
19     {
20         Button button = new Button(name);
21         gridbag.setConstraints(button, c);
22         add(button);
23     }
24 
25     public void init()
26     {
27         GridBagLayout gridbag = new GridBagLayout();
28         GridBagConstraints c = new GridBagConstraints();
29 
30         setFont(new Font("SansSerif", Font.PLAIN, 14));
31         setLayout(gridbag);
32 
33         c.fill = GridBagConstraints.BOTH;
34         c.weightx = 1.0;
35         makebutton("Button1", gridbag, c);
36         makebutton("Button2", gridbag, c);
37         makebutton("Button3", gridbag, c);
38 
39         c.gridwidth = GridBagConstraints.REMAINDER; //end row
40         makebutton("Button4", gridbag, c);
41 
42         c.weightx = 0.0; //reset to the default
43         makebutton("Button5", gridbag, c); //another row
44 
45         c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
46         makebutton("Button6", gridbag, c);
47 
48         c.gridwidth = GridBagConstraints.REMAINDER; //end row
49         makebutton("Button7", gridbag, c);
50 
51         c.gridwidth = 1; //reset to the default
52         c.gridheight = 2;
53         c.weighty = 1.0;
54         makebutton("Button8", gridbag, c);
55 
56         c.weighty = 0.0; //reset to the default
57         c.gridwidth = GridBagConstraints.REMAINDER; //end row
58         c.gridheight = 1; //reset to the default
59         makebutton("Button9", gridbag, c);
60         makebutton("Button10", gridbag, c);
61 
62         setSize(300, 100);
63     }
64 
65     public static void main(String args[])
66     {
67         JFrame f = new JFrame("GridBag Layout Example");
68         f.setLocation(400, 200);
69         GridBagEx2 ex1 = new GridBagEx2();
70 
71         ex1.init();
72 
73         f.add("Center", ex1);
74         f.pack();
75         f.setSize(f.getPreferredSize());
76         f.setVisible(true);
77         f.addWindowListener(new WindowAdapter()
78         {
79 
80             @Override
81             public void windowClosing(WindowEvent e)
82             {
83                 System.exit(0);
84             }
85             
86         });
87     }
88 
89 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM