Java 不仅能写服务器端程序,还可以用于创建桌面图形界面应用。常用的 GUI 库包括:
🎯 本文将重点介绍 Swing 和 JavaFX 的基本用法和项目实战。
组件 | 说明 |
---|---|
JFrame | 主窗口 |
JPanel | 面板容器 |
JLabel | 标签 |
JButton | 按钮 |
JTextField | 文本输入框 |
JTextArea | 多行文本输入 |
java复制编辑import javax.swing.*;
public class HelloSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing 示例");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("你好,Swing!");
frame.add(label);
frame.setVisible(true);
}
}
🖼️ 运行效果:
diff复制编辑+------------------------+
| 你好,Swing! |
+------------------------+
java复制编辑JButton button = new JButton("点击我");
button.addActionListener(e -> {
JOptionPane.showMessageDialog(frame, "按钮被点击了!");
});
frame.add(button);
📌 事件监听机制是 Swing 的核心,基于观察者模式。
Swing 提供多种布局方式:
布局管理器 | 说明 |
---|---|
FlowLayout | 流式排列,默认布局 |
BorderLayout | 上下左右中五个区域 |
GridLayout | 表格状布局 |
BoxLayout | 单行/单列组件排列 |
null | 自由布局(需 setBounds) |
示例:
java复制编辑frame.setLayout(new FlowLayout());
frame.add(new JButton("按钮1"));
frame.add(new JButton("按钮2"));
功能需求:
diff复制编辑+-----------------------------+
| 菜单栏:文件 编辑 |
+-----------------------------+
| |
| JTextArea |
| |
+-----------------------------+
java复制编辑public class SimpleNotepad {
public static void main(String[] args) {
JFrame frame = new JFrame("记事本");
JTextArea textArea = new JTextArea();
frame.add(new JScrollPane(textArea));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenuItem openItem = new JMenuItem("打开");
JMenuItem saveItem = new JMenuItem("保存");
JMenuItem exitItem = new JMenuItem("退出");
openItem.addActionListener(e -> {
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
try {
File file = chooser.getSelectedFile();
textArea.setText(Files.readString(file.toPath()));
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
saveItem.addActionListener(e -> {
JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
try {
File file = chooser.getSelectedFile();
Files.writeString(file.toPath(), textArea.getText());
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
exitItem.addActionListener(e -> System.exit(0));
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ 实现了基本的记事本功能,适合初学 Swing 项目开发。
对比点 | Swing | JavaFX |
---|---|---|
样式支持 | 较弱 | 支持 CSS |
UI 构建方式 | 代码手写 | 可使用 FXML 可视化 |
动画支持 | 基本 | 强大 |
性能表现 | 中等 | 更好 |
java复制编辑import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloFX extends Application {
@Override
public void start(Stage stage) {
Label label = new Label("你好,JavaFX!");
StackPane root = new StackPane(label);
Scene scene = new Scene(root, 400, 300);
stage.setTitle("JavaFX 示例");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
🖼️ 与 Swing 相比,结构更清晰,支持 CSS 美化和动画。
控件 | 说明 |
---|---|
Button | 按钮 |
TextField | 文本框 |
Label | 标签 |
TextArea | 多行文本 |
TableView | 表格控件 |
ListView | 列表视图 |
常见布局容器:
VBox
, HBox
(垂直/水平)
BorderPane
GridPane
StackPane
FXML 是 JavaFX 的 UI 描述语言,示例:
xml复制编辑<?xml version="1.0" encoding="UTF-8"?>
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="com.example.Controller">
<Label text="欢迎使用" />
<Button text="点击" onAction="#handleClick" />
</VBox>
控制器代码:
java复制编辑public class Controller {
public void handleClick() {
System.out.println("按钮被点击!");
}
}
less复制编辑+-----------------------------+
| 输入1:[ ] |
| 输入2:[ ] |
| [ + ] [ - ] [ * ] [ / ] |
| 结果:[ ] |
+-----------------------------+
java复制编辑public class CalculatorApp extends Application {
@Override
public void start(Stage stage) {
TextField input1 = new TextField();
TextField input2 = new TextField();
Label result = new Label("结果");
Button add = new Button("+");
add.setOnAction(e -> {
double a = Double.parseDouble(input1.getText());
double b = Double.parseDouble(input2.getText());
result.setText("结果: " + (a + b));
});
HBox buttons = new HBox(10, add, new Button("-"), new Button("*"), new Button("/"));
VBox root = new VBox(10, input1, input2, buttons, result);
Scene scene = new Scene(root, 300, 200);
stage.setScene(scene);
stage.setTitle("计算器");
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Platform.runLater
)
场景 | 推荐 |
---|---|
学习 / 跨平台桌面工具 | Swing |
需要现代界面、CSS 支持 | JavaFX |
企业级界面(大型系统) | JavaFX + FXML |
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。