35.md 5.7 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4 5 6
# Spring Boot Swing 集成教程

> 原文: [http://zetcode.com/articles/springbootswing/](http://zetcode.com/articles/springbootswing/)

在 Spring Boot Swing 集成教程中,我们将结合 Spring Boot 框架和 Swing 库。

W
wizardforcel 已提交
7
Spring 是流行的 Java 应用框架,而 Spring Boot 是 Spring 的演进,可以帮助轻松地创建独立的,生产级的基于 Spring 的应用。
W
wizardforcel 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Swing 是 Java 编程语言的主要 GUI 工具包。 Swing 完全用 Java 编写。

```java
pom.xml
src
├── main
   └── java
       └── com
           └── zetcode
               └── gui
                   └── SwingApp.java
└── test
    └── java

```

W
wizardforcel 已提交
25
这是 Spring Boot 应用的项目结构。
W
wizardforcel 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

`pom.xml`

```java
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zetcode</groupId>
    <artifactId>springbootswing</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

```

W
wizardforcel 已提交
75
这是 Maven 构建文件。 `spring-boot-starter`是包括自动配置支持,日志记录和 YAML 在内的核心启动器。 该应用打包到一个 JAR 文件中。
W
wizardforcel 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

`com/zetcode/gui/SwingApp.java`

```java
package com.zetcode.gui;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;

@SpringBootApplication
public class SwingApp extends JFrame {

    public SwingApp() {

        initUI();
    }

    private void initUI() {

        var quitButton = new JButton("Quit");

        quitButton.addActionListener((ActionEvent event) -> {
            System.exit(0);
        });

        createLayout(quitButton);

        setTitle("Quit button");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void createLayout(JComponent... arg) {

        var pane = getContentPane();
        var gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
        );
    }

    public static void main(String[] args) {

        var ctx = new SpringApplicationBuilder(SwingApp.class)
                .headless(false).run(args);

        EventQueue.invokeLater(() -> {

            var ex = ctx.getBean(SwingApp.class);
            ex.setVisible(true);
        });
    }
}

```

W
wizardforcel 已提交
147
这个简单的 Swing 应用的面板上有一个`JButton`。 单击该按钮可终止应用。
W
wizardforcel 已提交
148 149 150 151 152 153 154

```java
@SpringBootApplication
public class SwingApp extends JFrame {

```

W
wizardforcel 已提交
155
Swing 应用以`@SpringBootApplication`注解修饰。 注解启用 Spring Boot 服务。
W
wizardforcel 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

```java
var quitButton = new JButton("Quit");

```

在这里,我们创建一个按钮组件。 此构造函数将字符串标签作为参数。

```java
quitButton.addActionListener((ActionEvent event) -> {
    System.exit(0);
});

```

W
wizardforcel 已提交
171
我们将一个动作监听器插入按钮。 当我们单击按钮时,将调用侦听器的`actionPerformed()`方法。 该操作通过调用`System.exit()`方法来终止应用。
W
wizardforcel 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185

```java
createLayout(quitButton);

```

子组件需要放入容器中。 我们将任务委托给`createLayout()`方法。

```java
var gl = new GroupLayout(pane);
pane.setLayout(gl);

```

W
wizardforcel 已提交
186
我们使用`GroupLayout`进行应用布局。
W
wizardforcel 已提交
187 188 189 190 191 192 193

```java
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(SwingApp.class)
        .headless(false).run(args);

```

W
wizardforcel 已提交
194
Spring Boot 应用是使用`SpringApplicationBuilder`创建的。 我们关闭无头模式,该模式适用于服务器应用。
W
wizardforcel 已提交
195 196 197 198 199 200 201 202 203 204

```java
EventQueue.invokeLater(() -> {

    var ex = ctx.getBean(SwingApp.class);
    ex.setVisible(true);
});

```

W
wizardforcel 已提交
205
我们从应用上下文中检索 Swing 应用 bean。 `invokeLater()`方法将应用放置在 Swing 事件队列中。 它用于确保所有 UI 更新都是并发安全的。 换句话说,这是为了防止 GUI 在某些情况下挂起。
W
wizardforcel 已提交
206 207 208 209 210 211

```java
$ mvn spring-boot:run -q

```

W
wizardforcel 已提交
212
我们运行该应用。 `-q` Maven 选项关闭 Maven 消息。
W
wizardforcel 已提交
213

W
wizardforcel 已提交
214
在本教程中,我们使用 Spring Boot 框架创建了一个 Swing 应用。 您可能也对相关教程感兴趣: [Java Swing 教程](/tutorials/javaswingtutorial/)[独立的 Spring 应用](/articles/standalonespring/)[Java 教程](/lang/java/)或列出[所有 Spring Boot 教程](/all/#sprigboot) ]。