修飾子
java.awt.GridBagLayout
父類別
java.lang.Object
實作介面
LayoutManager
LayoutManager2
Serializable
建構子
GridBagLayout()
舉例如下
import java.awt.*;
import java.awt.event.*;
public class GridBagLayoutDemo {
public static void main(String[] args) {
new GridBagLayoutDemo();
}
public GridBagLayoutDemo() {
Frame frame = new Frame("AWTDemo");
frame.addWindowListener(new AdapterDemo());
frame.setLayout(new GridBagLayout());
Button b1 = new Button("one");
GridBagConstraints c1 = new GridBagConstraints();
c1.gridx = 0;
c1.gridy = 0;
c1.gridwidth = 1;
c1.gridheight = 1;
c1.fill = GridBagConstraints.BOTH;
c1.anchor = GridBagConstraints.CENTER;
frame.add(b1, c1);
Button b2 = new Button("two");
GridBagConstraints c2 = new GridBagConstraints();
c2.gridx = 1;
c2.gridy = 0;
c2.gridwidth = 1;
c2.gridheight = 1;
c2.fill = GridBagConstraints.BOTH;
c2.anchor = GridBagConstraints.CENTER;
frame.add(b2, c2);
Button b3 = new Button("three");
GridBagConstraints c3 = new GridBagConstraints();
c3.gridx = 0;
c3.gridy = 1;
c3.gridwidth = 2;
c3.gridheight = 1;
c3.fill = GridBagConstraints.BOTH;
c3.anchor = GridBagConstraints.CENTER;
frame.add(b3, c3);
frame.pack();
frame.setVisible(true);
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:GridBagLayoutDemo.java
功能:示範 Java 程式
作者:張凱慶
時間:西元 2012 年 3 月 */此例將 Frame 設定為 GridBagLayout ,然後建立三個按鈕放入 Frame 之中
Button b1 = new Button("one");
GridBagConstraints c1 = new GridBagConstraints();
c1.gridx = 0;
c1.gridy = 0;
c1.gridwidth = 1;
c1.gridheight = 1;
c1.fill = GridBagConstraints.BOTH;
c1.anchor = GridBagConstraints.CENTER;
frame.add(b1, c1);
Button b2 = new Button("two");
GridBagConstraints c2 = new GridBagConstraints();
c2.gridx = 1;
c2.gridy = 0;
c2.gridwidth = 1;
c2.gridheight = 1;
c2.fill = GridBagConstraints.BOTH;
c2.anchor = GridBagConstraints.CENTER;
frame.add(b2, c2);
Button b3 = new Button("three");
GridBagConstraints c3 = new GridBagConstraints();
c3.gridx = 0;
c3.gridy = 1;
c3.gridwidth = 2;
c3.gridheight = 1;
c3.fill = GridBagConstraints.BOTH;
c3.anchor = GridBagConstraints.CENTER;
frame.add(b3, c3);編譯後執行,結果如下

| 中英文術語對照 | |
|---|---|
| 類別 | class |
| 建構子 | constructor |
| 方法 | method |
您可以繼續參考
排版管理員
相關目錄
Java API 分類導覽
Java 教材
首頁
參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/GridBagLayout.html
1 則留言:
你好
我用了GridBagLayout的排版方式並把frame的大小設成300x600
我原本預計它會照著我設定的SIZE去分配相對應大小的GridBagConstraints
可是加進去的元件都被集中在中間一小塊
有甚麼辦法可以解決嗎@@?
張貼留言