Java Swing是Java语言中的一个GUI工具包,它提供了一系列的组件和容器,可以用于创建各种桌面应用程序。本教程将介绍Java Swing的基本概念、组件和容器,以及如何使用它们来创建一个简单的GUI应用程序。

一、Swing的基本概念

  1. 组件(Component):Swing中的组件是GUI界面中的基本元素,例如按钮、文本框、标签等。
  2. 容器(Container):Swing中的容器是一种特殊的组件,它可以包含其他组件,例如面板、框架等。
  3. 布局管理器(Layout Manager):Swing中的布局管理器用于控制组件在容器中的位置和大小,常用的布局管理器有FlowLayout、BorderLayout、GridLayout等。
  4. 事件(Event):Swing中的事件是用户与组件交互时发生的动作,例如点击按钮、输入文本等。
  5. 监听器(Listener):Swing中的监听器用于监听事件的发生,并执行相应的操作,例如点击按钮时执行某个方法。

二、Swing的组件

  1. 标签(JLabel):用于显示文本或图像。
JLabel label = new JLabel("Hello, World!");
  1. 按钮(JButton):用于触发事件。
JButton button = new JButton("Click me!");
button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        System.out.println("Button clicked!");
    }
});
  1. 文本框(JTextField):用于输入文本。
JTextField textField = new JTextField(20);
String text = textField.getText();
  1. 复选框(JCheckBox):用于选择一个或多个选项。
JCheckBox checkBox1 = new JCheckBox("Option 1");
JCheckBox checkBox2 = new JCheckBox("Option 2");
  1. 单选框(JRadioButton):用于选择一个选项。
JRadioButton radioButton1 = new JRadioButton("Option 1");
JRadioButton radioButton2 = new JRadioButton("Option 2");
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
  1. 下拉框(JComboBox):用于选择一个选项。
String[] options = {"Option 1", "Option 2", "Option 3"};
JComboBox comboBox = new JComboBox(options);
String selectedOption = (String) comboBox.getSelectedItem();
  1. 列表框(JList):用于显示一个列表。
String[] options = {"Option 1", "Option 2", "Option 3"};
JList list = new JList(options);
String selectedOption = (String) list.getSelectedValue();
  1. 滑动条(JSlider):用于选择一个数值。
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
int value = slider.getValue();
  1. 进度条(JProgressBar):用于显示一个进度。
JProgressBar progressBar = new JProgressBar();
progressBar.setValue(50);

三、Swing的容器

  1. 面板(JPanel):用于包含其他组件。
JPanel panel = new JPanel();
panel.add(new JLabel("Hello, World!"));
panel.add(new JButton("Click me!"));
  1. 框架(JFrame):用于创建一个窗口。
JFrame frame = new JFrame("My Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
  1. 对话框(JDialog):用于创建一个对话框。
JDialog dialog = new JDialog(frame, "My Dialog", true);
dialog.setSize(200, 100);
dialog.setVisible(true);

四、Swing的布局管理器

  1. 流式布局(FlowLayout):按照组件添加的顺序排列组件。
JPanel panel = new JPanel(new FlowLayout());
panel.add(new JLabel("Name:"));
panel.add(new JTextField(20));
panel.add(new JButton("Submit"));
  1. 边界布局(BorderLayout):将容器分为5个区域,分别为北、南、东、西和中。
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("North"), BorderLayout.NORTH);
panel.add(new JLabel("South"), BorderLayout.SOUTH);
panel.add(new JLabel("East"), BorderLayout.EAST);
panel.add(new JLabel("West"), BorderLayout.WEST);
panel.add(new JLabel("Center"), BorderLayout.CENTER);
  1. 网格布局(GridLayout):将容器分为若干行若干列的网格。
JPanel panel = new JPanel(new GridLayout(2, 2));
panel.add(new JLabel("1"));
panel.add(new JLabel("2"));
panel.add(new JLabel("3"));
panel.add(new JLabel("4"));

五、下面是一个使用Swing创建的简单GUI应用程序的完整示例代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyApplication {
    public static void main(String[] args) {
        JFrame frame = new JFrame("My Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        JPanel panel = new JPanel(new GridLayout(3, 2));
        panel.add(new JLabel("Name:"));
        panel.add(new JTextField(20));
        panel.add(new JLabel("Age:"));
        panel.add(new JTextField(20));
        panel.add(new JLabel("Gender:"));
        String[] genders = {"Male", "Female"};
        JComboBox comboBox = new JComboBox(genders);
        panel.add(comboBox);

        JButton button = new JButton("Submit");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String name = ((JTextField) panel.getComponent(1)).getText();
                String age = ((JTextField) panel.getComponent(3)).getText();
                String gender = (String) comboBox.getSelectedItem();
                JOptionPane.showMessageDialog(frame, "Name: " + name + "\nAge: " + age + "\nGender: " + gender);
            }
        });

        frame.add(panel, BorderLayout.CENTER);
        frame.add(button, BorderLayout.SOUTH);
        frame.setVisible(true);
    }
}

这个应用程序包含一个窗口,窗口中包含一个面板和一个按钮。面板中包含3个标签、3个文本框和一个下拉框。当用户点击按钮时,程序会获取文本框和下拉框中的值,并弹出一个对话框显示这些值。