Java Swing 简介
Java Swing 是一个用于构建桌面应用的图形用户界面(GUI)工具包,它完全基于 Java 语言,支持跨平台特性,可以在任何安装了 Java 运行环境的操作系统上运行。掌握 Java Swing 对于开发图形界面应用来说至关重要,它让开发者能够轻松地创建具有丰富用户交互功能的桌面应用程序。
环境准备
在开始学习之前,请确保以下环境已经准备就绪:
- Java 开发环境:JDK(Java Development Kit)是最基础的,需要配置环境变量。
- 集成开发环境(IDE):推荐使用 IntelliJ IDEA、Eclipse 或 NetBeans 等具有代码提示、调试等功能的 IDE。
- Java Swing 库:通常,JDK 包含了 Swing 库,无需额外安装。
Swing 组件基础
1. 窗口和面板
Swing 的窗口是应用程序的主要界面,而面板(Panel)用于组织和布局其他组件。
import javax.swing.*;
import java.awt.*;
public class MainWindow extends JFrame {
public MainWindow() {
setTitle("主窗口");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
this.add(mainPanel);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainWindow();
}
});
}
}
2. 控件
Swing 提供了丰富的控件,如按钮、文本框、复选框等。
import javax.swing.*;
import java.awt.*;
public class ControlsExample extends JFrame {
public ControlsExample() {
setTitle("控件示例");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("点击我");
JTextField textField = new JTextField(20);
JCheckBox checkBox = new JCheckBox("同意条款");
JPanel mainPanel = new JPanel(new GridLayout(3, 1));
mainPanel.add(button);
mainPanel.add(textField);
mainPanel.add(checkBox);
this.add(mainPanel);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ControlsExample();
}
});
}
}
3. 布局管理器
布局管理器负责控制组件在容器中的布局方式。常用的布局管理器有 FlowLayout、BorderLayout、GridLayout 和 GridBagLayout。
import javax.swing.*;
import java.awt.*;
public class LayoutExample extends JFrame {
public LayoutExample() {
setTitle("布局示例");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new BorderLayout());
JButton northButton = new JButton("北部按钮");
JButton southButton = new JButton("南部按钮");
JButton eastButton = new JButton("东部按钮");
JButton westButton = new JButton("西部按钮");
JButton centerButton = new JButton("中心按钮");
mainPanel.add(northButton, BorderLayout.NORTH);
mainPanel.add(southButton, BorderLayout.SOUTH);
mainPanel.add(eastButton, BorderLayout.EAST);
mainPanel.add(westButton, BorderLayout.WEST);
mainPanel.add(centerButton, BorderLayout.CENTER);
this.add(mainPanel);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new LayoutExample();
}
});
}
}
高级特性
1. 组件事件处理
Swing 允许你为组件添加事件监听器,以实现与用户的交互。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ActionListenerExample extends JFrame {
public ActionListenerExample() {
setTitle("事件监听器示例");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("点击我");
JLabel label = new JLabel("尚未点击");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("已点击");
}
});
JPanel mainPanel = new JPanel(new FlowLayout());
mainPanel.add(button);
mainPanel.add(label);
this.add(mainPanel);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ActionListenerExample();
}
});
}
}
2. 资源文件和国际化
Swing 支持资源文件,便于进行应用程序的国际化。
import javax.swing.*;
import java.awt.*;
import java.util.ResourceBundle;
public class InternationalizationExample extends JFrame {
private static final ResourceBundle messages = ResourceBundle.getBundle("Messages");
public InternationalizationExample() {
setTitle(messages.getString("windowTitle"));
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton(messages.getString("buttonText"));
JLabel label = new JLabel(messages.getString("labelText"));
JPanel mainPanel = new JPanel(new FlowLayout());
mainPanel.add(button);
mainPanel.add(label);
this.add(mainPanel);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new InternationalizationExample();
}
});
}
}
在 Messages.properties 文件中定义如下内容:
windowTitle=国际化的窗口
buttonText=点击我
labelText=尚未点击
总结
通过以上内容,你应该已经对 Java Swing 有了一个初步的了解。掌握 Swing 的基本概念和组件,并能够运用布局管理器、事件处理和资源文件等高级特性,你将能够轻松地创建出功能丰富的桌面应用程序。不断实践,探索更多可能性,你将成为一个出色的 Swing 开发者!