Java 的图形用户界面(GUI)编程允许我们构建可视化桌面应用,比如:
尽管 Web 应用和移动端占据主流,但桌面端仍广泛应用于内网工具和企业应用。Java 提供了两套 GUI 开发库:
本篇我们将结合理论与代码图示,全面讲解 Java GUI 编程的开发流程与实践案例。
技术 | 说明 | 优点 | 缺点 |
---|---|---|---|
AWT | Java GUI 最早库 | 轻量,简单 | 功能少,兼容差 |
Swing | 更强 GUI 库 | 组件丰富,平台无关 | 比较老旧,界面风格传统 |
JavaFX | 现代 GUI 库 | 样式美观,支持动画 | 学习成本较高 |
SWT | Eclipse GUI 库 | 运行高效,原生样式 | 对平台绑定较强 |
📷 图示:AWT 窗口结构
mathematica复制编辑Frame
├── Button
├── Label
└── TextField
java复制编辑import java.awt.*;
public class AWTDemo {
public static void main(String[] args) {
Frame frame = new Frame("AWT 示例");
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
Label label = new Label("请输入姓名:");
TextField tf = new TextField(20);
Button btn = new Button("提交");
frame.add(label);
frame.add(tf);
frame.add(btn);
frame.setVisible(true);
}
}
💡 使用
FlowLayout
可让组件从左到右自动排布。
Swing 是建立在 AWT 之上的增强库,组件更丰富,功能更强。
📷 Swing 窗口结构图
markdown复制编辑JFrame
└── JPanel
├── JLabel
├── JTextField
└── JButton
java复制编辑import javax.swing.*;
public class SwingLogin {
public static void main(String[] args) {
JFrame frame = new JFrame("登录窗口");
frame.setSize(350, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("用户名:");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("密码:");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100, 50, 165, 25);
panel.add(passwordText);
JButton loginButton = new JButton("登录");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
}
}
📷 效果图:
markdown复制编辑-----------------------
| 用户名: [ ] |
| 密 码: [ ] |
| [登录] |
-----------------------
Java GUI 的响应机制基于事件监听器。典型的流程:
📷 事件模型图
复制编辑用户操作 → 事件对象 → 注册的监听器 → 响应方法
java复制编辑button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了!");
}
});
📌 常见监听器包括:
ActionListener
:按钮点击
MouseListener
:鼠标事件
KeyListener
:键盘事件
WindowListener
:窗口状态
📷 图示:Swing 计算器界面结构
markdown复制编辑JFrame
└── JPanel (GridLayout)
├── JTextField(结果框)
├── JButton:0~9, + - * / = 清除
java复制编辑import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator {
private static JTextField display = new JTextField();
private static double result = 0;
private static String operator = "=";
private static boolean start = true;
public static void main(String[] args) {
JFrame frame = new JFrame("简易计算器");
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
display.setEditable(false);
frame.add(display, BorderLayout.NORTH);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"
};
for (String text : buttons) {
JButton btn = new JButton(text);
btn.addActionListener(new ButtonListener());
panel.add(btn);
}
frame.add(panel);
frame.setVisible(true);
}
static class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String input = e.getActionCommand();
if ("0123456789".contains(input)) {
if (start) {
display.setText(input);
} else {
display.setText(display.getText() + input);
}
start = false;
} else {
if (input.equals("C")) {
result = 0;
operator = "=";
display.setText("");
start = true;
} else {
calculate(Double.parseDouble(display.getText()));
operator = input;
start = true;
}
}
}
private void calculate(double x) {
switch (operator) {
case "+": result += x; break;
case "-": result -= x; break;
case "*": result *= x; break;
case "/": result /= x; break;
case "=": result = x; break;
}
display.setText("" + result);
}
}
}
📷 运行效果:
markdown复制编辑-----------------
| 123 + 456 =579 |
| [7][8][9][ / ] |
| [4][5][6][ * ] |
| [1][2][3][ - ] |
| [0][C][=][ + ] |
-----------------
布局 | 说明 | 使用示例 |
---|---|---|
FlowLayout | 顺序排列 | new FlowLayout() |
BorderLayout | 东南西北中 | frame.add(comp, BorderLayout.SOUTH) |
GridLayout | 网格布局 | new GridLayout(3, 2) |
null | 自定义位置 | 使用 setBounds() 手动设置 |
CardLayout
切换不同视图
MVC
模式管理界面与数据
问题 | 解答 |
---|---|
字体显示不清晰 | 调整 JComponent.setFont() |
程序窗口无法关闭 | 设置 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) |
布局混乱 | 熟悉布局管理器组合使用 |
响应无效 | 检查 addActionListener() 是否正确注册 |
通过本篇文章,我们系统学习了 Java GUI 的开发路径,包括:
✅ AWT 与 Swing 的区别与用途 ✅ 创建窗口、表单、响应用户操作 ✅ 实战开发一个计算器界面 ✅ 图文结构 + 完整代码让你快速上手桌面开发
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。