- Swing 程式設計例子
- 例子 - 主頁
- 例子 - 環境設定
- 例子 - 邊框
- 例子 - 按鈕
- 例子 - 複選框
- 例子 - 組合框
- 例子 - 顏色選擇器
- 例子 - 對話方塊
- 例子 - 編輯器面板
- 例子 - 檔案選擇器
- 例子 - 格式文字欄位
- 例子 - 框架
- 例子 - 列表
- 例子 - 佈局
- 例子 - 選單
- 例子 - 密碼欄位
- 例子 - 進度條
- 例子 - 滾動面板
- 例子 - 滑塊
- 例子 - 微調器
- 例子 - 表格
- 例子 - 工具欄
- 例子 - 樹
- Swing 有用資源
- Swing - 快速指南
- Swing - 有用資源
- Swing - 討論
Swing 例子 - 網格包佈局
這是最靈活的佈局管理器類。GridBagLayout 的物件按垂直、水平或與基線對齊元件,而不要求元件具有相同的尺寸。
例子
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SwingTester {
public static void main(String[] args) {
createWindow();
}
private static void createWindow() {
JFrame frame = new JFrame("Swing Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createUI(frame);
frame.setSize(560, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void createUI(final JFrame frame){
JPanel panel = new JPanel();
GridBagLayout layout = new GridBagLayout();
panel.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JButton("Button 1"),gbc);
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(new JButton("Button 2"),gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 20;
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(new JButton("Button 3"),gbc);
gbc.gridx = 1;
gbc.gridy = 1;
panel.add(new JButton("Button 4"),gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
panel.add(new JButton("Button 5"),gbc);
frame.getContentPane().add(panel, BorderLayout.CENTER);
}
}
輸出
swingexamples_layouts.htm
廣告