Java Swing 基础组件详解

2026-08-03 09:19:55 世界杯大力神杯

Java Swing 是一个功能强大的 GUI 工具包,提供了丰富的组件库用于构建跨平台的桌面应用程序。本文将详细讲解 Swing 的基础组件,包括其作用、使用方法以及示例代码,帮助你快速掌握 Swing 的核心知识。

一、什么是 Swing 基础组件?Swing 的基础组件是构建用户界面的核心元素,用于展示信息或与用户进行交互。所有 Swing 的组件都继承自 javax.swing.JComponent 类,这使得它们具有一致的行为和特性。

二、常见的 Swing 基础组件1. JLabel:标签组件

作用:用于显示一段文本、图像或两者的组合,不能被用户编辑。常用方法: setText(String text):设置标签显示的文本。setIcon(Icon icon):设置标签显示的图标。setHorizontalAlignment(int alignment):设置水平对齐方式。示例:

代码语言:javascript复制import javax.swing.*;

public class JLabelExample {

public static void main(String[] args) {

JFrame frame = new JFrame("JLabel Example");

frame.setSize(300, 200);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label = new JLabel("Hello, Swing!");

label.setHorizontalAlignment(SwingConstants.CENTER); // 居中对齐

frame.add(label);

frame.setVisible(true);

}

}2. JButton:按钮组件

作用:用于触发某些操作(如事件处理)。常用方法: setText(String text):设置按钮上的文本。addActionListener(ActionListener l):为按钮添加点击事件监听器。示例:

代码语言:javascript复制3. JTextField:单行文本框

作用:用于输入或显示单行文本。常用方法: getText():获取文本框中的文本。setText(String text):设置文本框的内容。setColumns(int columns):设置文本框的列数(宽度)。示例:

4. JTextArea:多行文本框

作用:用于输入或显示多行文本。常用方法: append(String text):在文本框末尾追加文本。setLineWrap(boolean wrap):设置是否自动换行。setWrapStyleWord(boolean word):设置按单词换行。示例:

代码语言:javascript复制import javax.swing.*;

public class JTextAreaExample {

public static void main(String[] args) {

JFrame frame = new JFrame("JTextArea Example");

frame.setSize(400, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTextArea textArea = new JTextArea(10, 30);

textArea.setLineWrap(true); // 自动换行

textArea.setWrapStyleWord(true); // 按单词换行

JButton button = new JButton("Submit");

button.addActionListener(e -> JOptionPane.showMessageDialog(frame, "You entered:\n" + textArea.getText()));

frame.add(new JScrollPane(textArea), java.awt.BorderLayout.CENTER); // 添加滚动条

frame.add(button, java.awt.BorderLayout.SOUTH);

frame.setVisible(true);

}

}5. JCheckBox:复选框

作用:用于启用或禁用选项,可以选择多个复选框。常用方法: isSelected():检查复选框是否被选中。setSelected(boolean selected):设置复选框的初始状态。示例:

代码语言:javascript复制import javax.swing.*;

public class JCheckBoxExample {

public static void main(String[] args) {

JFrame frame = new JFrame("JCheckBox Example");

frame.setSize(300, 200);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JCheckBox checkBox1 = new JCheckBox("Option 1");

JCheckBox checkBox2 = new JCheckBox("Option 2");

JButton button = new JButton("Submit");

button.addActionListener(e -> {

String result = "Selected: ";

if (checkBox1.isSelected()) result += "Option 1 ";

if (checkBox2.isSelected()) result += "Option 2 ";

JOptionPane.showMessageDialog(frame, result);

});

frame.setLayout(new java.awt.FlowLayout());

frame.add(checkBox1);

frame.add(checkBox2);

frame.add(button);

frame.setVisible(true);

}

}6. JRadioButton:单选按钮

作用:用于一组互斥选项。常用方法: isSelected():检查单选按钮是否被选中。使用 ButtonGroup 将多个按钮分组。示例:

代码语言:javascript复制代码语言:javascript复制import javax.swing.*;

public class JRadioButtonExample {

public static void main(String[] args) {

JFrame frame = new JFrame("JRadioButton Example");

frame.setSize(300, 200);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JRadioButton option1 = new JRadioButton("Option 1");

JRadioButton option2 = new JRadioButton("Option 2");

ButtonGroup group = new ButtonGroup();

group.add(option1);

group.add(option2);

JButton button = new JButton("Submit");

button.addActionListener(e -> {

if (option1.isSelected()) {

JOptionPane.showMessageDialog(frame, "Option 1 selected");

} else if (option2.isSelected()) {

JOptionPane.showMessageDialog(frame, "Option 2 selected");

}

});

frame.setLayout(new java.awt.FlowLayout());

frame.add(option1);

frame.add(option2);

frame.add(button);

frame.setVisible(true);

}

}三、总结Swing 的基础组件是构建桌面应用程序的核心。通过合理使用这些组件,可以快速实现用户输入、交互和结果展示功能。