Java API 分類導覽 - java.awt.GridBagConstraints gridx



GridBagConstraints 類別 (class) 的 gridx 屬性 (field) 用來設定元件在由左往右第幾行。



修飾子
public int gridx


舉例如下
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
屬性field


您可以繼續參考
排版管理員


相關目錄
Java API 分類導覽
Java 教材
首頁


參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/GridBagConstraints.html

沒有留言: