Swing 是 Java 純 Java 實現的輕量級 GUI 工具包,基於 AWT 擴展,不依賴本地操作系統。本文通過三個完整 Demo 實戰掌握 Swing 核心開發模式。
技術點:JFrame、GridBagLayout 網格包布局、JLabel/JTextField/JButton、居中顯示
public class SwingDemo1 extends JPanel {
private static final int W = 300, H = 150;
public SwingDemo1() {
JFrame frame = new JFrame("登錄窗口");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
frame.add(this, BorderLayout.WEST);
frame.setSize(W, H);
centerWindow(frame);
// 組件創建
JLabel tip = new JLabel("歡迎來到Java世界");
JLabel userLabel = new JLabel("用戶名:");
JLabel pwdLabel = new JLabel("密碼:");
JTextField userText = new JTextField(15);
JTextField pwdText = new JPasswordField(15); // 建議用密碼框
JButton submit = new JButton("登錄");
JButton cancel = new JButton("取消");
// GridBagConstraints 精確定位
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 3; gbc.weighty = 4;
addComp(tip, gbc, 0, 0, 4, 1);
addComp(userLabel, gbc, 0, 1, 1, 1);
addComp(pwdLabel, gbc, 0, 2, 1, 1);
addComp(userText, gbc, 2, 1, 1, 1);
addComp(pwdText, gbc, 2, 2, 1, 1);
addComp(submit, gbc, 0, 3, 1, 1);
addComp(cancel, gbc, 2, 3, 1, 1);
frame.setResizable(false);
frame.setVisible(true);
}
private void addComp(Component c, GridBagConstraints gbc, int x, int y, int w, int h) {
gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = w; gbc.gridheight = h;
add(c, gbc);
}
private void centerWindow(JFrame f) {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation((d.width - W)/2, (d.height - H)/2);
}
public static void main(String[] args) { new SwingDemo1(); }
}

技術點:JMenuBar/JMenu/JMenuItem、JToolBar、BorderLayout
public class SwingDemo2 {
private static final int W = 600, H = 400;
public SwingDemo2() {
JFrame frame = new JFrame("菜單工具欄");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(W, H);
JPanel panel = new JPanel();
frame.setContentPane(panel);
// 菜單欄
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("菜單1"), m2 = new JMenu("菜單2");
m1.add(new JMenuItem("子菜單11")); m1.addSeparator(); m1.add(new JMenuItem("子菜單12"));
m2.add(new JMenuItem("子菜單21")); m2.addSeparator(); m2.add(new JMenuItem("子菜單22"));
mb.add(m1); mb.add(m2);
frame.setJMenuBar(mb);
// 工具欄
JToolBar tb = new JToolBar();
tb.add(new JButton("工具1"));
tb.add(new JButton("工具2"));
tb.add(new JButton("工具3"));
panel.setLayout(new BorderLayout());
panel.add(tb, BorderLayout.NORTH);
centerWindow(frame);
frame.setResizable(false);
frame.setVisible(true);
}
// centerWindow 同 Demo1
public static void main(String[] args) { new SwingDemo2(); }
}

技術點:JTabbedPane 繼承、addTab/setEnabledAt/setTitleAt、滾動佈局策略
public class SwingDemo3 extends JTabbedPane {
private static final int W = 600, H = 400;
public SwingDemo3() {
JFrame frame = new JFrame("Tab頁簽");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(W, H);
frame.setVisible(true);
JPanel panel = new JPanel();
frame.setContentPane(panel);
setLayout(new BorderLayout());
JPanel p1 = new JPanel(new BorderLayout());
JPanel p2 = new JPanel(new BorderLayout());
addTab("panel1", p1); setEnabledAt(0, true); setTitleAt(0, "測試頁1");
addTab("panel2", p2); setEnabledAt(1, true); setTitleAt(1, "測試頁2");
setPreferredSize(new Dimension(500, 200));
setTabPlacement(JTabbedPane.TOP);
setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); // 多標籤滾動
panel.add(this, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) { new SwingDemo3(); }
}

SwingUtilities.invokeLater(() -> new Demo())JPasswordField、JFormattedTextField 等專用組件BorderLayout 整體 + GridBagLayout 細節dispose()、監聽器註銷、定時器停止三大 Demo 覆蓋 Swing 最核心的窗口、佈局、菜單、工具欄、標籤頁開發場景。掌握佈局管理器組合、EDT 線程模型、組件模型是 Swing 開發進階基石。