?? testgridbag.java
字號:
import java.awt.*;
/**
* Description:
* <br/>Copyright (C), 2005-2008, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class TestGridBag
{
private Frame f = new Frame("測試窗口");
private GridBagLayout gb = new GridBagLayout();
private GridBagConstraints gbc = new GridBagConstraints();
private Button[] bs = new Button[10];
public void init()
{
f.setLayout(gb);
for (int i = 0; i < bs.length ; i++ )
{
bs[i] = new Button("按鈕" + i);
}
//所有組件都可以橫向、縱向上擴大
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
addButton(bs[0]);
addButton(bs[1]);
addButton(bs[2]);
//該GridBagConstraints控制的GUI組件將會成為橫向最后一個元素
gbc.gridwidth = GridBagConstraints.REMAINDER;
addButton(bs[3]);
//該GridBagConstraints控制的GUI組件將橫向上不會擴大
gbc.weightx = 0;
addButton(bs[4]);
//該GridBagConstraints控制的GUI組件將橫跨2個網(wǎng)格
gbc.gridwidth = 2;
addButton(bs[5]);
//該GridBagConstraints控制的GUI組件將橫跨1個網(wǎng)格
gbc.gridwidth = 1;
//該GridBagConstraints控制的GUI組件將縱向跨2個網(wǎng)格
gbc.gridheight = 2;
//該GridBagConstraints控制的GUI組件將會成為橫向最后一個元素
gbc.gridwidth = GridBagConstraints.REMAINDER;
addButton(bs[6]);
//該GridBagConstraints控制的GUI組件將橫向跨越一個網(wǎng)格,縱向跨越2個網(wǎng)格。
gbc.gridwidth = 1;
gbc.gridheight = 2;
//該GridBagConstraints控制的GUI組件縱向擴大的權(quán)重是1
gbc.weighty = 1;
addButton(bs[7]);
//設(shè)置下面的按鈕在縱向上不會擴大
gbc.weighty = 0;
//該GridBagConstraints控制的GUI組件將會成為橫向最后一個元素
gbc.gridwidth = GridBagConstraints.REMAINDER;
//該GridBagConstraints控制的GUI組件將縱向上橫跨1個網(wǎng)格
gbc.gridheight = 1;
addButton(bs[8]);
addButton(bs[9]);
f.pack();
f.setVisible(true);
}
private void addButton(Button button)
{
gb.setConstraints(button, gbc);
f.add(button);
}
public static void main(String[] args)
{
new TestGridBag().init();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -