- Swing 程式設計示例
- 示例 - 首頁
- 示例 - 環境設定
- 示例 - 邊框
- 示例 - 按鈕
- 示例 - 複選框
- 示例 - 組合框
- 示例 - 顏色選擇器
- 示例 - 對話方塊
- 示例 - 編輯器窗格
- 示例 - 檔案選擇器
- 示例 - 格式化文字框
- 示例 - 框架
- 示例 - 列表
- 示例 - 佈局
- 示例 - 選單
- 示例 - 密碼框
- 示例 - 進度條
- 示例 - 滾動窗格
- 示例 - 滑塊
- 示例 - 微調框
- 示例 - 表格
- 示例 - 工具欄
- 示例 - 樹
- Swing 實用資源
- Swing - 快速指南
- Swing - 有用資源
- Swing - 討論
Swing 示例 - Spring 佈局
**SpringLayout** 類根據一組約束條件對關聯容器的子級進行定位。
示例
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
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();
SpringLayout layout = new SpringLayout();
panel.setLayout(layout);
JLabel label = new JLabel("Enter Name: ");
JTextField textField = new JTextField("", 15);
panel.add(label);
panel.add(textField);
layout.putConstraint(SpringLayout.WEST, label,5, SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, label,5, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, textField,5, SpringLayout.EAST, label);
layout.putConstraint(SpringLayout.NORTH, textField,5, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.EAST, panel,5, SpringLayout.EAST, textField);
layout.putConstraint(SpringLayout.SOUTH, panel,5, SpringLayout.SOUTH, textField);
frame.getContentPane().add(panel, BorderLayout.CENTER);
}
}
輸出
swingexamples_layouts.htm
廣告