絕對佈局 (setLayout(null)) 雖精確控制坐標,但:
**佈局管理器** 以約束規則描述組件相對關係,容器自動計算幾何屬性,**實現響應式適配**。本文演示 BorderLayout + FlowLayout 組合構建經典「頂部查詢區、中間表格區、底部操作區」界面。
JFrame (BorderLayout)
├── NORTH: JPanel (FlowLayout LEFT) → 查詢條件:Label + TextField + Button
├── CENTER: JPanel (GridLayout 1x1) → JScrollPane → JTable
└── SOUTH: JPanel (FlowLayout Hgap=30) → 操作按鈕:新增 / 刪除 / 修改
JFrame 設置 BorderLayout (默認即是)。FlowLayout (默認居中,需改 LEFT)。GridLayout(1,1) 撐滿 JScrollPane。FlowLayout.alignment = LEFT → 組件左對齊。FlowLayout.hgap / vgap → 組件水平/垂直間距 (SOUTH 按鈕間距 30px)。Component.preferredSize → 建議尺寸 (Label 100x16, TextField 140x22)。public class VideoQueryFrame extends JFrame {
private JPanel northPanel, centerPanel, southPanel;
private JLabel queryLabel;
private JTextField queryField;
private JButton queryBtn, addBtn, deleteBtn, updateBtn;
private JScrollPane scrollPane;
private JTable dataTable;
public VideoQueryFrame() {
initComponents();
setupLayout();
bindEvents();
}
private void initComponents() {
northPanel = new JPanel(); northPanel.setBackground(Color.RED);
centerPanel = new JPanel(); centerPanel.setBackground(Color.PINK);
southPanel = new JPanel(); southPanel.setBackground(Color.ORANGE);
queryLabel = new JLabel("請輸入查詢條件");
queryField = new JTextField();
queryBtn = new JButton("查詢");
addBtn = new JButton("增加");
deleteBtn = new JButton("刪除");
updateBtn = new JButton("修改");
dataTable = new JTable();
scrollPane = new JScrollPane(dataTable);
}
private void setupLayout() {
// 根佈局
setLayout(new BorderLayout());
// NORTH: FlowLayout LEFT
FlowLayout northLayout = new FlowLayout(FlowLayout.LEFT, 5, 10);
northPanel.setLayout(northLayout);
northPanel.add(queryLabel);
northPanel.add(queryField);
northPanel.add(queryBtn);
queryLabel.setPreferredSize(new Dimension(100, 16));
queryField.setPreferredSize(new Dimension(140, 22));
add(northPanel, BorderLayout.NORTH);
// CENTER: GridLayout 1x1 撐滿 ScrollPane
centerPanel.setLayout(new GridLayout(1, 1));
centerPanel.add(scrollPane);
add(centerPanel, BorderLayout.CENTER);
// SOUTH: FlowLayout Hgap=30
FlowLayout southLayout = new FlowLayout(FlowLayout.CENTER, 30, 5);
southPanel.setLayout(southLayout);
southPanel.add(addBtn);
southPanel.add(deleteBtn);
southPanel.add(updateBtn);
add(southPanel, BorderLayout.SOUTH);
// 窗口屬性
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
}
private void bindEvents() {
queryBtn.addActionListener(e -> loadMockData());
}
private void loadMockData() {
// 模擬 30 列 x 100 行數據
Vector columns = new Vector<>();
for (int i = 1; i <= 30; i++) columns.add("列" + i);
Vector> rows = new Vector<>();
for (int r = 1; r <= 100; r++) {
Vector
| 管理器 | 核心規則 | 典型用途 | 響應式行為 |
|---|---|---|---|
| BorderLayout | 5 區域 (N/W/C/E/S),CENTER 自動填滿剩餘空間 | 主視窗整體結構 | CENTER 隨窗口拉伸,邊緣區域固定 preferredSize |
| FlowLayout | 左到右、上到下流式排列,支持對齊/間距 | 工具欄、按鈕組、表單行 | 寬度不足自動換行,組件尺寸固定 |
| GridLayout | 固定行列網格,單元格等大 | 計算器鍵盤、表格標題行 | 單元格均分容器空間 |
| GridBagLayout | 網格袋:組件可跨行列、權重分配、錨點、填充 | 複雜表單、精細對齊 | 極其靈活,但 API 繁瑣 |
| BoxLayout | 單行/單列盒子,支持膠水/支柱彈性空間 | 垂直表單、工具欄分組 | 彈性空間吸收剩餘尺寸 |
| MigLayout (第三方) | 字符串約束 DSL,功能最強、代碼最簡潔 | 生產級複雜界面首選 | 完美響應式,學習曲線平緩 |
// 表單行:Label 固定寬,Field 撐滿剩餘
JPanel row = new JPanel(new BorderLayout(5, 0));
row.add(new JLabel("用戶名:"), BorderLayout.WEST);
row.add(new JTextField(), BorderLayout.CENTER);
// 工具欄分組:左邊按鈕組、右邊彈性空間、右邊幫助按鈕
JPanel toolbar = new JPanel(new BorderLayout());
JPanel leftGroup = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
leftGroup.add(new JButton("新建"));
leftGroup.add(new JButton("保存"));
toolbar.add(leftGroup, BorderLayout.WEST);
toolbar.add(Box.createHorizontalGlue(), BorderLayout.CENTER); // 吸收中間空間
JPanel rightGroup = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
rightGroup.add(new JButton("幫助"));
toolbar.add(rightGroup, BorderLayout.EAST);
FlowLayout,BorderLayout 區域放 JPanel 時**必須顯式設置其佈局**,否則子組件不按預期排列。preferredSize;需限制縮放範圍時覆寫 getMinimumSize()/getMaximumSize()。revalidate() (觸發佈局) + repaint() (觸發繪製)。SwingUtilities.invokeLater()。掌握 BorderLayout (宏觀結構) + FlowLayout (微觀流式) + GridLayout (等分網格) 三大基礎佈局管理器的**組合嵌套**,即可覆蓋 80% 企業級桌面應用界面需求。關鍵心法:
BorderLayout.CENTER、Box.createGlue()、GridBagConstraints.weightx 吸收變化。對於極度複雜界面,引入 MigLayout 或遷移 **JavaFX (FXML + CSS + 布局面板)** 獲得更現代的開發體驗。但理解 Swing 佈局核心原理,仍是 Java GUI 工程師的必修內功。