- Swing 程式設計示例
- 示例 - 主頁
- 示例 - 環境設定
- 示例 - 邊框
- 示例 - 按鈕
- 示例 - 複選框
- 示例 - 組合框
- 示例 - 顏色選取器
- 示例 - 對話方塊
- 示例 - 編輯器窗格
- 示例 - 檔案選取器
- 示例 - 格式化文字欄位
- 示例 - 框架
- 示例 - 列表
- 示例 - 佈局
- 示例 - 選單
- 示例 - 密碼欄位
- 示例 - 進度條
- 示例 - 滾動窗格
- 示例 - 滑塊
- 示例 - 旋轉器
- 示例 - 表格
- 示例 - 工具欄
- 示例 - 樹
- Swing 實用資源
- Swing - 快速指南
- Swing - 實用資源
- Swing - 討論
Swing 示例 - 分組佈局
類 GroupLayout 按層次對控制元件進行分組,以便將它們放置在容器中。
示例
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.GroupLayout;
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();
GroupLayout layout = new GroupLayout(panel);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");
JButton btn3 = new JButton("Button 3");
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(btn1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addComponent(btn2)
.addComponent(btn3))));
layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(btn1)
.addComponent(btn2)
.addComponent(btn3));
panel.setLayout(layout);
frame.getContentPane().add(panel, BorderLayout.CENTER);
}
}
輸出
swingexamples_layouts.htm
廣告