上一篇
java怎么写图形化
- 后端开发
- 2025-09-01
- 25
Java中,可以使用Swing或JavaFX库来创建图形化界面,
Swing是Java自带的GUI工具包,而JavaFX是一个更现代的、功能更强大的GUI框架,以下是一个简单的例子:,“`java,import javax.swing.;,public class SimpleGUI {, public static void main(String[] args) {, JFrame frame = new JFrame(“Simple GUI”);, frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);, frame.setSize(300, 200);, frame.setVisible(true);, },},
Java 图形化编程指南
Java 提供了多种方式来实现图形化界面,其中最常用的是 Swing 和 JavaFX,本文将详细介绍如何使用这两种技术创建图形化应用程序。
Swing 基础
JFrame 主窗口
所有 Swing 应用程序都从创建一个 JFrame 开始,它是应用程序的主窗口。
import javax.swing.JFrame;
public class MyFirstSwingApp {
public static void main(String[] args) {
// 创建 JFrame 实例
JFrame frame = new JFrame("我的第一个 Swing 应用程序");
// 设置默认关闭操作
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口大小
frame.setSize(400, 300);
// 设置窗口可见
frame.setVisible(true);
}
}
JPanel 内容面板
JPanel 是一个轻量级容器,用于组织和管理组件。
import javax.swing.;
import java.awt.;
public class PanelExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JPanel 示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
// 创建 JPanel
JPanel panel = new JPanel();
// 设置布局管理器
panel.setLayout(new FlowLayout());
// 添加按钮到面板
JButton button1 = new JButton("按钮1");
JButton button2 = new JButton("按钮2");
panel.add(button1);
panel.add(button2);
// 将面板添加到框架
frame.add(panel);
frame.setVisible(true);
}
}
布局管理器
Swing 提供了多种布局管理器来控制组件的排列方式:
| 布局管理器 | 说明 |
|---|---|
FlowLayout |
按顺序水平排列组件 |
BorderLayout |
将容器分为五个区域(北、南、东、西、中) |
GridLayout |
网格布局,所有组件大小相同 |
GridBagLayout |
更灵活的网格布局 |
BoxLayout |
垂直或水平排列组件 |
import javax.swing.;
import java.awt.;
public class LayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("布局管理器示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
// 使用 BorderLayout
frame.setLayout(new BorderLayout());
JButton northButton = new JButton("北边");
JButton southButton = new JButton("南边");
JButton eastButton = new JButton("东边");
JButton westButton = new JButton("西边");
JButton centerButton = new JButton("中间");
frame.add(northButton, BorderLayout.NORTH);
frame.add(southButton, BorderLayout.SOUTH);
frame.add(eastButton, BorderLayout.EAST);
frame.add(westButton, BorderLayout.WEST);
frame.add(centerButton, BorderLayout.CENTER);
frame.setVisible(true);
}
}
事件处理
Swing 使用事件监听器来处理用户交互。
import javax.swing.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class EventHandlingExample {
public static void main(String[] args) {
JFrame frame = new JFrame("事件处理示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
JButton button = new JButton("点击我");
// 添加 ActionListener
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "按钮被点击了!");
}
});
frame.add(button);
frame.setVisible(true);
}
}
JavaFX 基础
JavaFX 应用程序结构
JavaFX 应用程序需要继承 Application 类并重写 start 方法。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class MyFirstJavaFXApp extends Application {
@Override
public void start(Stage primaryStage) {
// 创建标签组件
Label label = new Label("Hello, JavaFX!");
// 创建场景
Scene scene = new Scene(label, 400, 300);
// 设置舞台标题
primaryStage.setTitle("我的第一个 JavaFX 应用程序");
// 将场景设置到舞台
primaryStage.setScene(scene);
// 显示舞台
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
布局容器
JavaFX 提供了多种布局容器:
| 布局容器 | 说明 |
|---|---|
HBox |
水平排列子节点 |
VBox |
垂直排列子节点 |
BorderPane |
分为 top, bottom, left, right, center |
GridPane |
网格布局 |
StackPane |
堆叠布局 |
TilePane |
瓷砖式布局 |
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class JavaFXLayoutExample extends Application {
@Override
public void start(Stage primaryStage) {
// 创建 HBox 布局
HBox hbox = new HBox();
// 添加按钮到 HBox
Button btn1 = new Button("按钮1");
Button btn2 = new Button("按钮2");
hbox.getChildren().addAll(btn1, btn2);
// 创建场景并设置到舞台
Scene scene = new Scene(hbox, 300, 100);
primaryStage.setTitle("JavaFX 布局示例");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
CSS 样式
JavaFX 支持使用 CSS 来美化界面。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class JavaFXCSSExample extends Application {
@Override
public void start(Stage primaryStage) {
// 创建按钮
Button button = new Button("样式化按钮");
// 创建布局容器
StackPane root = new StackPane();
root.getChildren().add(button);
// 创建场景并应用 CSS
Scene scene = new Scene(root, 300, 200);
scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
primaryStage.setTitle("JavaFX CSS 示例");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
styles.css 文件内容:
.root {
-fx-background-color: #f0f8ff;
}
.button {
-fx-background-color: #4682b4;
-fx-text-fill: white;
-fx-font-size: 16px;
-fx-padding: 10px 20px;
}
Swing vs JavaFX 对比
| 特性 | Swing | JavaFX |
|---|---|---|
| 出现时间 | 1997年 | 2007年 |
| 架构风格 | AWT 扩展,重量级组件 | 现代 UI 框架,轻量级组件 |
| CSS 支持 | 有限支持 | 完整支持 |
| FXML 支持 | 无 | 有,用于声明式 UI |
| 3D 支持 | 基本无 | 内置支持 |
| 动画支持 | 需要额外库 | 内置支持 |
| 性能 | 较低 | 较高 |
| 现代特性 | 较少 | 丰富(如 CSS、FXML、动画等) |
实战案例:简单计算器
Swing 版本
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalculatorSwing extends JFrame {
private JTextField display;
private JPanel buttonPanel;
public CalculatorSwing() {
setTitle("简单计算器 Swing");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 400);
display = new JTextField();
display.setEditable(false);
display.setFont(new Font("Arial", Font.BOLD, 24));
add(display, BorderLayout.NORTH);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "",
"1", "2", "3", "-",
"0", "C", "=", "+"
};
for (String text : buttons) {
JButton button = new JButton(text);
button.addActionListener(new ButtonClickListener());
buttonPanel.add(button);
}
add(buttonPanel, BorderLayout.CENTER);
setVisible(true);
}
private class ButtonClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String command = ((JButton)e.getSource()).getText();
// 这里应该实现计算逻辑,简化为显示按钮文本
display.setText(display.getText() + command);
}
}
public static void main(String[] args) {
new CalculatorSwing();
}
}
JavaFX 版本
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.stage.Stage; import javafx.scene.input.MouseEvent; import javafx.event.EventHandler; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundFill; import javafx.geometry.Pos; import javafx.scene.layout.CornerRadii; import javafx.scene.paint.Paint; import javafx.scene.shape.Circle; import javafx.scene.shape.Rectangle; import javafx.scene.shape.Shape; import javafx.scene.Group; import javafx.animation.AnimationTimer; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.util.Duration; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.scene.effect.DropShadow; import javafx.scene.text.TextAlignment; import javafx.scene.control.ScrollPane; import javafx.collections.ObservableList; import javafx.scene.Node; import javafx.scene.image.ImageView; import javafx.scene.image.Image; import javafx.scene.control.ToggleButton; import javafx.scene.control.ToggleGroup; import javafx.scene.control.RadioButton; import javafx.scene.control.CheckBox; import javafx.scene.control.Slider; import javafx.scene.control.Spinner; import javafx.scene.control.ColorPicker; import javafx.scene.control.DatePicker; import javafx.scene.control.ComboBox; import javafx.scene.control.ListView; import javafx.scene.control.TableView; import javafx.scene.control.TreeView; import javafx.scene.control.TabPane; import javafx.scene.control.SplitPane; import javafx.scene.control.TitledPane; import javafx.scene.control.Accordion; import javafx.scene.control.MenuBar; import javafx.scene.control.Menu; import javafx.scene.control.MenuItem; import javafx.scene.control.ContextMenu; import javafx.scene.control.Tooltip; import javafx.scene.control.Separator; import javafx.scene.control.Labeled; import javafx.scene.control.SkinBase; import javafx.scene.control.Cell; import javafx.scene.control.IndexedCell; import javafx.scene.control.TableColumn; import javafx.scene.control.TableRow; import javafx.scene.control.TablePosition; import javafx.scene.control.TableCell; import javafx.scene.control.MultipleSelectionModel; import javafx.scene.control.FocusModel; import javafx.scene.control.EditEvent; import javafx.scene.control.ScrollTopLeftInteraction; import javafx.scene.control.
