# 使用Swing开发Java GUI程序

使用Swing开发Java GUI程序

# 使用IDEA拖拽开发界面

  1. 在项目包中点击Swing UI设计器 输入窗体名称就可以创建成功

1656778490529

  1. 慢慢拖拽即可创建窗体

1656778670646

  1. 具体可参考 IDEA快速GUI界面 (opens new window) 如何利用Intellij idea的GUI Designer设计GUI (opens new window)

  2. 打包所需依赖

 		<dependency>
                <groupId>com.intellij</groupId>
                <artifactId>forms_rt</artifactId>
                <version>7.0.3</version>
        </dependency>
1
2
3
4
5

# 如何美化开发的页面

美化前:

1656778926076

美化后:

1656778959760

# 要点

  1. 要使用最新版的jdk 新版jdk对UI进行了美化

  2. 使用主题依赖

<dependency>
    <groupId>com.formdev</groupId>
    <artifactId>flatlaf</artifactId>
    <version>2.3</version>
</dependency>

<dependency>
    <groupId>com.formdev</groupId>
    <artifactId>flatlaf-intellij-themes</artifactId>
    <version>2.3</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11

​ 在主窗口实例化之前调用

// 主题设置
FlatIntelliJLaf.install();
UIManager.put("TextComponent.arc", 5);
UIManager.put("Component.focusWidth", 1);
UIManager.put("Component.innerFocusWidth", 1);
UIManager.put("Button.innerFocusWidth", 1);
UIManager.put("TitlePane.unifiedBackground", true);
UIManager.put("TitlePane.menuBarEmbedded", false);
1
2
3
4
5
6
7
8
  1. 设置字体,设置字体抗锯齿
// 字体设置
// 抗锯齿 
System.setProperty("awt.useSystemAAFontSettings", "on");
System.setProperty("swing.aatext", "true");
Font fontUIResource = new Font("宋体", Font.PLAIN, 22);
for (Enumeration keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
    Object key = keys.nextElement();
    Object value = UIManager.get(key);
    if (value instanceof FontUIResource) {
        UIManager.put(key, fontUIResource);
    }
}
UIManager.put("defaultFont", fontUIResource);
1
2
3
4
5
6
7
8
9
10
11
12
13

# 如何解决打包后静态资源无法访问

使用springboot构建Swing程序,然后将静态资源放入resources

然后使用

  ClassLoader loader = Thread.currentThread().getContextClassLoader();   
  InputStream msyh = loader.getResourceAsStream(name);
1
2

怎么使用springboot构建Swing程序呢?

@SpringBootApplication
public class SourceDocxApplication implements CommandLineRunner {

    public static void main(String[] args) {
        new SpringApplicationBuilder(SourceDocxApplication.class)
                .headless(false).run(args);
    }

    @Override
    public void run(String... args) throws Exception {
        Ui.getInstance().initUI();


    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Ui.java要继承JFrame

    public static Ui getInstance() {
        if (null == instance) {
            synchronized (Ui.class) {
                if (null == instance) {
                    instance = new Ui();
                }
            }
        }
        return instance;
    }
    
    public void initUI() throws Exception {
       ...窗体设置代码
       // 主窗口设置
        JFrame frame = new JFrame("软著源代码生成器");
        frame.setContentPane(new Ui().jPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);

    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

打包配置

<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<includeSystemScope>true</includeSystemScope>
				</configuration>
			</plugin>
		</plugins>
			<!-- 打包配置 -->
		<resources> 
            <resource>  
	              <directory>src/main/resources</directory>  
	                <includes>
	                    <include>**/*</include>  
	                </includes>  
            </resource>
		</resources>
	</build>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20