本文鏈接: http://blog.csdn.net/xietansheng/article/details/72814541
Java Swing 圖形界面開發(目錄)
1. 概述
官方JavaDocsApi: java.awt.FlowLayout
FlowLayout,流式佈局管理器。按水平方向依次排列放置組件,排滿一行,換下一行繼續排列。排列方向(左到右 或 右到左)取決於容器的componentOrientation屬性(該屬性屬於Component),它可能的值如下:
ComponentOrientation.LEFT_TO_RIGHT(默認)
ComponentOrientation.RIGHT_TO_LEFT
同一行(水平方向)的組件的對齊方式由 FlowLayout 的align屬性確定,它可能的值如下:
FlowLayout.LEFT : 左對齊
FlowLayout.CENTER : 居中對齊(默認)
FlowLayout.RIGHT : 右對齊
FlowLayout.LEADING : 與容器方向的開始邊對齊,例如,對於從左到右的方向,則與左邊對齊
FlowLayout.TRAILING : 與容器方向的結束邊對齊,例如,對於從左到右的方向,則與右邊對齊。
FlowLayout的 構造方法:
// 默認 居中對齊的,水平和垂直間隙是 5 個單位
FlowLayout()
// 指定對齊方式,默認的水平和垂直間隙是 5 個單位
FlowLayout(int align)
// 指定對其方式,水平 和 豎直 間隙
FlowLayout(int align, int hgap, int vgap)
2. 代碼實例
package com.xiets.swing;
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame jf = new JFrame("測試窗口");
jf.setSize(200, 250);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
// 創建內容面板,指定使用 流式佈局
JPanel panel = new JPanel(new FlowLayout());
JButton btn01 = new JButton("按鈕01");
JButton btn02 = new JButton("按鈕02");
JButton btn03 = new JButton("按鈕03");
JButton btn04 = new JButton("按鈕04");
JButton btn05 = new JButton("按鈕05");
panel.add(btn01);
panel.add(btn02);
panel.add(btn03);
panel.add(btn04);
panel.add(btn05);
jf.setContentPane(panel);
jf.setVisible(true); // PS: 最後再設置爲可顯示(繪製), 所有添加的組件纔會顯示
}
}