Java 的佈局管理器GridBagLayout的使用方法【圖文】

 GridBagLayout是java裏面最重要的佈局管理器之一,可以做出很複雜的佈局,可以說GridBagLayout是必須要學好的的,

 
GridBagLayout 類是一個靈活的佈局管理器,它不要求組件的大小相同便可以將組件垂直、水平或沿它們的基線對齊。
 
每個 GridBagLayout 對象維持一個動態的矩形單元網格,每個組件佔用一個或多個這樣的單元,該單元被稱爲顯示區域。
 
下面就通過一個記事本案例去說明GridBagLayout的使用方法。
 
 
 
分析:
 
帶有箭頭的說明可以拉伸的。
 
4佔用4個格子,6佔用4個格子。如果設置6可以拉伸了,那麼4也會跟着拉伸。
 
但是如果設置4拉伸,那麼7所在的列也可以拉伸,所以4不能設置拉伸。我們應該設置4是跟隨6進行拉伸。
 
灰色的線是爲了看清佈局的大概,組件佔用的格子數。
 

 

  1. import java.awt.*; 
  2. import javax.swing.*; 
  3.  
  4. public class GridBagDemo extends JFrame { 
  5.     public static void main(String args[]) { 
  6.         GridBagDemo demo = new GridBagDemo(); 
  7.     } 
  8.  
  9.     public GridBagDemo() { 
  10.         init(); 
  11.         this.setSize(600,600); 
  12.         this.setVisible(true); 
  13.     } 
  14.     public void init() { 
  15.         j1 = new JButton("打開"); 
  16.         j2 = new JButton("保存"); 
  17.         j3 = new JButton("另存爲"); 
  18.         j4 = new JPanel(); 
  19.         String[] str = { "java筆記""C#筆記""HTML5筆記" }; 
  20.         j5 = new JComboBox(str); 
  21.         j6 = new JTextField(); 
  22.         j7 = new JButton("清空"); 
  23.         j8 = new JList(str); 
  24.         j9 = new JTextArea(); 
  25.      j9.setColor.PINK);//爲了看出效果,設置了顏色 
  26.         GridBagLayout layout = new GridBagLayout(); 
  27.         this.setLayout(layout); 
  28.         this.add(j1);//把組件添加進jframe 
  29.         this.add(j2); 
  30.         this.add(j3); 
  31.         this.add(j4); 
  32.         this.add(j5); 
  33.         this.add(j6); 
  34.         this.add(j7); 
  35.         this.add(j8); 
  36.         this.add(j9); 
  37.         GridBagConstraints s= new GridBagConstraints();//定義一個GridBagConstraints, 
  38.         //是用來控制添加進的組件的顯示位置 
  39.         s.fill = GridBagConstraints.BOTH; 
  40.         //該方法是爲了設置如果組件所在的區域比組件本身要大時的顯示情況 
  41.         //NONE:不調整組件大小。 
  42.         //HORIZONTAL:加寬組件,使它在水平方向上填滿其顯示區域,但是不改變高度。 
  43.         //VERTICAL:加高組件,使它在垂直方向上填滿其顯示區域,但是不改變寬度。 
  44.         //BOTH:使組件完全填滿其顯示區域。 
  45.         s.gridwidth=1;//該方法是設置組件水平所佔用的格子數,如果爲0,就說明該組件是該行的最後一個 
  46.         s.weightx = 0;//該方法設置組件水平的拉伸幅度,如果爲0就說明不拉伸,不爲0就隨着窗口增大進行拉伸,0到1之間 
  47.         s.weighty=0;//該方法設置組件垂直的拉伸幅度,如果爲0就說明不拉伸,不爲0就隨着窗口增大進行拉伸,0到1之間 
  48.         layout.setConstraints(j1, s);//設置組件 
  49.         s.gridwidth=1
  50.         s.weightx = 0
  51.         s.weighty=0
  52.         layout.setConstraints(j2, s); 
  53.         s.gridwidth=1
  54.         s.weightx = 0
  55.         s.weighty=0
  56.         layout.setConstraints(j3, s); 
  57.         s.gridwidth=0;//該方法是設置組件水平所佔用的格子數,如果爲0,就說明該組件是該行的最後一個 
  58.         s.weightx = 0;//不能爲1,j4是佔了4個格,並且可以橫向拉伸, 
  59.         //但是如果爲1,後面行的列的格也會跟着拉伸,導致j7所在的列也可以拉伸 
  60.         //所以應該是跟着j6進行拉伸 
  61.         s.weighty=0
  62.         layout.setConstraints(j4, s) 
  63.         ;s.gridwidth=2
  64.         s.weightx = 0
  65.         s.weighty=0
  66.         layout.setConstraints(j5, s); 
  67.         ;s.gridwidth=4
  68.         s.weightx = 1
  69.         s.weighty=0
  70.         layout.setConstraints(j6, s); 
  71.         ;s.gridwidth=0
  72.         s.weightx = 0
  73.         s.weighty=0
  74.         layout.setConstraints(j7, s); 
  75.         ;s.gridwidth=2
  76.         s.weightx = 0
  77.         s.weighty=1
  78.         layout.setConstraints(j8, s); 
  79.         ;s.gridwidth=5
  80.         s.weightx = 0
  81.         s.weighty=1
  82.         layout.setConstraints(j9, s); 
  83.     } 
  84.     JButton j1; 
  85.     JButton j2; 
  86.     JButton j3; 
  87.     JPanel j4; 
  88.     JComboBox j5; 
  89.     JTextField j6; 
  90.     JButton j7; 
  91.     JList j8; 
  92.     JTextArea j9; 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章