Java API 分類導覽 - java.awt.FlowLayout

FlowLayout 類別 (class) 用來建立流動式的版面編排物件。。



所謂流動式的版面編排,意謂元件以加入順序排列,第一列排滿了就放到第二列,依此類推。


修飾子
public class FlowLayout


父類別
java.lang.Object


實作介面
LayoutManager
Serializable


建構子
FlowLayout()
FlowLayout(int align)
FlowLayout(int align, int hgap, int vgap)


常用屬性
名稱敘述
CENTER設定元件置中對齊
LEADING設定元件依排列方式對齊
LEFT設定元件向左對齊
RIGHT設定元件向右對齊
TRAILING設定元件依最後一個元件對齊


舉例如下
import java.awt.*;
import java.awt.event.*;

public class FlowLayoutDemo {
    public static void main(String[] args) {
        Frame frame = new Frame("AWTDemo");
        frame.addWindowListener(new AdapterDemo());
        frame.setLayout(new GridLayout(5, 1));
        frame.setSize(420, 520);
        
        Panel p1 = new Panel(new FlowLayout(FlowLayout.CENTER));
        p1.setSize(400, 100);
        Button center = new Button("center");
        p1.add(center);
        Panel p2 = new Panel(new FlowLayout(FlowLayout.LEADING));
        p2.setSize(400, 100);
        Button leading = new Button("leading");
        p2.add(leading);
        Panel p3 = new Panel(new FlowLayout(FlowLayout.LEFT));
        p3.setSize(400, 100);
        Button left = new Button("left");
        p3.add(left);
        Panel p4 = new Panel(new FlowLayout(FlowLayout.RIGHT));
        p4.setSize(400, 100);
        Button right = new Button("right");
        p4.add(right);
        Panel p5 = new Panel(new FlowLayout(FlowLayout.TRAILING));
        p5.add(left);
        Button trailing = new Button("trailing");
        p5.add(trailing);
                
        frame.add(p1);
        frame.add(p2);
        frame.add(p3);
        frame.add(p4);
        frame.add(p5);
        
        frame.setVisible(true);
    }
}

class AdapterDemo extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:FlowLayoutDemo.java
    功能:示範 Java 程式 
    作者:張凱慶
    時間:西元 2012 年 3 月 */


此例建立五個 Panel 型態物件,依序設定為 FlowLayout 的置中對齊、依排列方式對齊、向左對齊、向右對齊、依最後一個元件對齊,然後每個 Panel 放入一個 Button
Panel p1 = new Panel(new FlowLayout(FlowLayout.CENTER));
p1.setSize(400, 100);
Button center = new Button("center");
p1.add(center);
Panel p2 = new Panel(new FlowLayout(FlowLayout.LEADING));
p2.setSize(400, 100);
Button leading = new Button("leading");
p2.add(leading);
Panel p3 = new Panel(new FlowLayout(FlowLayout.LEFT));
p3.setSize(400, 100);
Button left = new Button("left");
p3.add(left);
Panel p4 = new Panel(new FlowLayout(FlowLayout.RIGHT));
p4.setSize(400, 100);
Button right = new Button("right");
p4.add(right);
Panel p5 = new Panel(new FlowLayout(FlowLayout.TRAILING));
p5.add(left);
Button trailing = new Button("trailing");


編譯後執行,結果如下



中英文術語對照
類別class
建構子constructor
方法method


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


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


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

沒有留言: