Java 的布局管理器GridBagLayout的使用方法

來源:互聯網
上載者:User

標籤:

 

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

GridBagLayout 類是一個靈活的布局管理器,它不要求組件的大小相同便可以將組件垂直、水平或沿它們的基準對齊。

每個 GridBagLayout 對象維持一個動態矩形單元網格,每個組件佔用一個或多個這樣的單元,該單元被稱為顯示地區。

下面就通過一個記事本案例去說明GridBagLayout的使用方法。

 

分析:

帶有箭頭的說明可以展開的。

4佔用4個格子,6佔用4個格子。如果設定6可以展開了,那麼4也會跟著展開。

但是如果設定4展開,那麼7所在的列也可以展開,所以4不能設定展開。我們應該設定4是跟隨6進行展開。

灰色的線是為了看清布局的大概,組件佔用的格子數。

運行時的顯示效果

import java.awt.*;import javax.swing.*;public class GridBagDemo extends JFrame {    public static void main(String args[]) {        GridBagDemo demo = new GridBagDemo();    }    public GridBagDemo() {        init();        this.setSize(600,600);        this.setVisible(true);    }    public void init() {        j1 = new JButton("開啟");        j2 = new JButton("儲存");        j3 = new JButton("另存新檔");        j4 = new JPanel();        String[] str = { "java筆記", "C#筆記", "HTML5筆記" };        j5 = new JComboBox(str);        j6 = new JTextField();        j7 = new JButton("清空");        j8 = new JList(str);        j9 = new JTextArea();      j9.setBackground(Color.PINK);//為了看出效果,設定了顏色        GridBagLayout layout = new GridBagLayout();        this.setLayout(layout);        this.add(j1);//把組件添加進jframe        this.add(j2);        this.add(j3);        this.add(j4);        this.add(j5);        this.add(j6);        this.add(j7);        this.add(j8);        this.add(j9);        GridBagConstraints s= new GridBagConstraints();//定義一個GridBagConstraints,        //是用來控制添加進的組件的顯示位置        s.fill = GridBagConstraints.BOTH;        //該方法是為了設定如果組件所在的地區比組件本身要大時的顯示情況        //NONE:不調整組件大小。        //HORIZONTAL:加寬組件,使它在水平方向上填滿其顯示地區,但是不改變高度。        //VERTICAL:加高組件,使它在垂直方向上填滿其顯示地區,但是不改變寬度。        //BOTH:使組件完全填滿其顯示地區。        s.gridwidth=1;//該方法是設定組件水平所佔用的格子數,如果為0,就說明該組件是該行的最後一個        s.weightx = 0;//該方法設定組件水平的展開幅度,如果為0就說明不展開,不為0就隨著視窗增大進行展開,0到1之間        s.weighty=0;//該方法設定組件垂直的展開幅度,如果為0就說明不展開,不為0就隨著視窗增大進行展開,0到1之間        layout.setConstraints(j1, s);//設定組件        s.gridwidth=1;        s.weightx = 0;        s.weighty=0;        layout.setConstraints(j2, s);        s.gridwidth=1;        s.weightx = 0;        s.weighty=0;        layout.setConstraints(j3, s);        s.gridwidth=0;//該方法是設定組件水平所佔用的格子數,如果為0,就說明該組件是該行的最後一個        s.weightx = 0;//不能為1,j4是佔了4個格,並且可以橫向展開,        //但是如果為1,後面行的列的格也會跟著展開,導致j7所在的列也可以展開        //所以應該是跟著j6進行展開        s.weighty=0;        layout.setConstraints(j4, s)        ;s.gridwidth=2;        s.weightx = 0;        s.weighty=0;        layout.setConstraints(j5, s);        ;s.gridwidth=4;        s.weightx = 1;        s.weighty=0;        layout.setConstraints(j6, s);        ;s.gridwidth=0;        s.weightx = 0;        s.weighty=0;        layout.setConstraints(j7, s);        ;s.gridwidth=2;        s.weightx = 0;        s.weighty=1;        layout.setConstraints(j8, s);        ;s.gridwidth=5;        s.weightx = 0;        s.weighty=1;        layout.setConstraints(j9, s);    }    JButton j1;    JButton j2;    JButton j3;    JPanel j4;    JComboBox j5;    JTextField j6;    JButton j7;    JList j8;    JTextArea j9;}

http://www.cnblogs.com/taoweiji/archive/2012/12/14/2818787.html

Java 的布局管理器GridBagLayout的使用方法(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.