java佈局學習 (二)

前文中介紹了FlowLayout和BorderLayout 本文咱們將會繼續介紹java中的佈局方式html

(3)GridLayout 網格佈局java

這種佈局會將整個容器劃分紅M行*N列的網格。框架

以下圖:ide

                                                                                                     
       
       
       
       
       
       
       

 

 

 

 

由模型圖咱們能夠知道這種佈局,相似於咱們常見的掃雷、計算器等軟件的佈局。函數

 

這種佈局的構造函數有三種佈局

 

1 GridLayout()  //一行一列 
2 
3 GridLayout(int rows, int cols) 4 
5 GridLayout(int rows, int cols, int hgap, int vgap)//hgap 水平間距, vgap垂直間距

 

在向這種容器中添加控件時,會以向左後右,先上後下的順序添加(防盜鏈接:本文首發自http://www.cnblogs.com/jilodream/ )控件。 而不能在指定的位置中添加控件,換言之控件之間不能留有空白。 下面咱們來看代碼spa

 1 import java.awt.*;  2 import javax.swing.*;  3 
 4 class GridFrame extends JFrame  5 {  6     JPanel panel=new JPanel(new GridLayout(4,4,3,3));//構造指定佈局的容器
 7     String str[]={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};  8     public GridFrame(String name)  9  { 10         super(name); 11         setLayout(new BorderLayout()); 12  JButton btnArray[]; 13         btnArray=new JButton[str.length]; 14         for(int i=0;i<str.length;i++) 15  { 16             btnArray[i]=new JButton(str[i]); 17  panel.add(btnArray[i]); 18  } 19         setVisible(true); 20         setSize(250,200); 21  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 22         setVisible(true); 23  } 24     
25      public static void main(String[] args) 26  { 27         GridFrame gf=new GridFrame("網格佈局計算機!"); 28  } 29 
30 }  

顯示效果以下圖
code

 

 

(4)GridBagLayoutcomponent

GridBagLayout也是一種表格,可是能夠經過設定規則更自由的將控件綁定到容器上:htm

以下圖:

 

將容器切割爲若干個小的格子Cell,而後向這些Cell中添加控件,同時能夠在某一個(防盜鏈接:本文首發自http://www.cnblogs.com/jilodream/ )方向上連續的幾個Cell進行拼接,組成一個大的Cell放置控件。

操做步驟以下:

(1)new GridBagLayout() 設置到指定容器上。

(2)GridBagConstraint  gbc 新增這樣的一個容器規則。(注意Constraint是規則、約束、條件的意思)

設定的參數規則以下

gridx  gridy 索引

gridwidth gridheight 跨越的索引

fill  是否動態擴充

weightx、weighty 擴大的權重

(3)gb.setConstraints(控件,gbc)  將控件和規則綁定起來

(4)constainer.add(c) 添加容器,而後將控件添加到容器上。

同時gbc 能夠重用,反覆的設定骨子額,綁定規則,添加容器

重複 二、三、4步

 請參照 以下代碼 ,重點注意 addComponent()方法:

 1 public class NumberPad {
 2   private static final Insets insets = new Insets(0, 0, 0, 0);
 3   public static void main(final String args[]) {
 4     final JFrame frame = new JFrame("NumberPad");
 5     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口操做
 6     frame.setLayout(new GridBagLayout());//框架佈局
 7     JButton button;
 8     //下面利用設立的類對按鍵進行佈局
 9     //第一行
10     button = new JButton("Num");
11     addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
12     button = new JButton("/");
13     addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
14     button = new JButton("*");
15     addComponent(frame, button, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
16     button = new JButton("-");
17     addComponent(frame, button, 3, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
18     //第二行
19     button = new JButton("1");
20     addComponent(frame, button, 0, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
21     button = new JButton("2");
22     addComponent(frame, button, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
23     button = new JButton("3");
24     addComponent(frame, button, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
25     button = new JButton("+");
26     addComponent(frame, button, 3, 1, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
27     // 第三行
28     button = new JButton("4");
29     addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
30     button = new JButton("5");
31     addComponent(frame, button, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
32     button = new JButton("6");
33     addComponent(frame, button, 2, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
34     //第四行
35     button = new JButton("7");
36     addComponent(frame, button, 0, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
37     button = new JButton("8");
38     addComponent(frame, button, 1, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
39     button = new JButton("9");
40     addComponent(frame, button, 2, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
41     button = new JButton("Enter");
42     addComponent(frame, button, 3, 3, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
43     //第五行
44     button = new JButton("0");
45     addComponent(frame, button, 0, 4, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
46     button = new JButton(".");
47     addComponent(frame, button, 2, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);  
48     frame.setSize(250,250);
49     frame.setVisible(true);
50   }
51   
52     private static void addComponent(Container container, Component component, int gridx, int gridy,
53       int gridwidth, int gridheight, int anchor, int fill) {
54     GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
55         anchor, fill, insets, 0, 0);//創建網格包對象
56     container.add(component, gbc);//添加到容器中
57   }

ps 此代碼來自http://blog.sina.com.cn/s/blog_6cea57330100pwvq.html

這裏有幾點要注意的

(1)控制動態擴充的fill參數,有四個值分別爲 水平擴充、垂直擴充、水平垂直擴充、不擴充。你們根據本身的實際狀況來選擇  

(2)weightx、weighty 是擴大的權重,這個權重表示的是,當橫向(防盜鏈接:本文首發自http://www.cnblogs.com/jilodream/ )或縱向擴大N的倍數後,那麼他佔的比重是多少。這裏要當心,就是權重大的話,若是往大拖容器size,那麼這個控件增加的快,同時當size變小時,權重大的也縮小的更快。因此若是要想讓權重大的控件始終大時,須要將幾個控件的初始size設定的很是小,這樣全部的控件都是在拖大的狀態了。

相關文章
相關標籤/搜索