關於Java中GridBagLayout布局管理器的用法。

來源:互聯網
上載者:User

GidBagLayout:
(1)GridBagConstraints.gridwidthGridBagConstraints.gridheight
指定組件的顯示地區行(針對 gridwidth)或列(針對 gridheight)中的單元數。預設值為 1。如下向視窗中添加一個佔兩個儲存格(兩行一列)的按鈕的例子:
JFrame f=new JFrame();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
f.setLayout(gridbag);
c.gridheight=2;
c.gridwidth=1;
JButton jButton = new JButton("按鈕1");
gridbag.setConstraints(button, c);
f.add(jButton);
(2)GridBagConstraints.fill
當組件的顯示地區大於組件的所需大小時,用於確定是否(以及如何)調整組件。
可能的值為 GridBagConstraints.NONE(預設值)、
GridBagConstraints.HORIZONTAL(加寬組件直到它足以在水平方向上填滿其顯示地區,但不更改其高度)、

GridBagConstraints.VERTICAL(加高組件直到它足以在垂直方向上填滿其顯示地區,但不更改其寬度)和

GridBagConstraints.BOTH(使組件完全填滿其顯示地區)。
使用情景舉例:在一個很大的視窗(如300*300)中添加一個按鈕(原始大小40*30)。

(3)GridBagConstraints.anchor
當組件小於其顯示地區時,用於確定將組件置於何處(在顯示地區中)。可能的值有兩種:相對和絕對。相對值的解釋是相對於容器的ComponentOrientation 屬性,而絕對值則不然。個人覺得只使用絕對值就可以。有效值有:

絕對值
GridBagConstraints.NORTH
GridBagConstraints.SOUTH
GridBagConstraints.WEST
GridBagConstraints.EAST
GridBagConstraints.NORTHWEST
GridBagConstraints.NORTHEAST
GridBagConstraints.SOUTHWEST
GridBagConstraints.SOUTHEAST
GridBagConstraints.CENTER (the default)

(4)GridBagConstraints.weightx、GridBagConstraints.weighty (************最重要的屬性)
用於確定分布空間的方式,這對於指定調整行為至關重要。例如:在一個很大的視窗(如300*300)中添加兩個按鈕(也可以是面板)(原始大小40*30),預設的,你會發現兩個按鈕分別處於上下兩個等大小的地區中,且只佔用了一小部分,沒有被按鈕佔用的地區就被稱為額外地區。該額外地區會隨著參數weightx、weighty而被分配。

完整的範例程式碼如下:

import javax.swing.*;
import java.util.*;
import java.awt.*;

public class Example{

public Example() {
}

public static void main(String args[]) {
JFrame f = new JFrame("GridBag Layout Example");

GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
f.setLayout(gridbag);
//添加按鈕1
c.fill = GridBagConstraints.BOTH;
c.gridheight=2;
c.gridwidth=1;
c.weightx=0.0;//預設值為0.0
c.weighty=0.0;//預設值為0.0
c.anchor=GridBagConstraints.SOUTHWEST;
JButton jButton1 = new JButton("按鈕1");
gridbag.setConstraints(jButton1, c);
f.add(jButton1);
//添加按鈕2
c.fill = GridBagConstraints.NONE;
c.gridwidth=GridBagConstraints.REMAINDER;
c.gridheight=1;
c.weightx=1.0;//預設值為0.0
c.weighty=0.8;
JButton jButton2 = new JButton("按鈕2");
gridbag.setConstraints(jButton2, c);
f.add(jButton2);
//添加按鈕3
c.fill = GridBagConstraints.BOTH;
c.gridwidth=1;
c.gridheight=1;
c.weighty=0.2;
JButton jButton3 = new JButton("按鈕3");
gridbag.setConstraints(jButton3, c);
f.add(jButton3);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,500);
f.setVisible(true);
}
}

在上述代碼中添加按鈕2時c.weighty=0.8,而在添加按鈕3時c.weighty=0.2,這就會導致按鈕2所佔地區的高大約是按鈕3所佔地區的高的0.8/0.2=4倍。

轉載:http://sword03.blog.sohu.com/26246269.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.