使用springboot搭建swing桌面程序(一)

🎈概述

在习惯使用springboot开发代码之后,在我们自己进行桌面程序编码的时候,发现很多的不方便,比如连接数据库,读取配置文件,循环依赖,日志记录,定时任务等等。这篇文章详细介绍了使用springboot搭建一个桌面程序easytodo,一个桌面便签和任务,使用jpa+h2作为本地存储,打包成windows应用。

🤖项目介绍

  • 支持快速添加任务
  • 快速完成状态更新
  • 任务支持分类
  • 支持统计
  • 桌面展示
  • 静态展示,单一颜色,防止审美疲劳

🦜UI设计

使用figma工具设计的,具体页面地址 easytodo页面设计,设计要求是颜色冷淡为主,主要颜色是淡蓝色和白色,用橙色作为点缀😏。设计了三个logo,个人比较喜欢第一个,可惜已经别人用了,最后采用的第三个,我没有去抄袭别人的图标,确实就是按照功能直接设计出来的,圆形打个勾勒表示任务完成。
在这里插入图片描述

🐎主要代码

废话不多说直接上代码😎
在这里插入图片描述

mainFrame

package com.mucong.easytodo.ui;

import com.formdev.flatlaf.extras.FlatSVGUtils;
import com.mucong.easytodo.constant.ColorTheme;
import com.mucong.easytodo.ui.component.FootPane;
import com.mucong.easytodo.ui.component.MainPane;
import com.mucong.easytodo.ui.component.TitlePane;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


@Component
public class MainFrame extends JFrame {
    private int x;
    private int y;
    public int width;
    public int heght;
    public int dpi;

    @Autowired
    private TrayPopMenu trayPopMenu;


    public MainFrame() throws HeadlessException {
        //计算窗口起始大小和位置,中间位置,
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        dpi = Toolkit.getDefaultToolkit().getScreenResolution();
        width = 3*dpi;
        heght = 2*width;
        if(heght>screen.getHeight()){
            heght =(int)( 0.8 * screen.getHeight());
            width = heght/2;
        }
        x = (int)screen.getWidth()/2 - width/2;
        y = (int)screen.getHeight()/2 - heght/2;
        this.setBounds(x,y,width,heght);
        this.setUndecorated(true);
        this.setOpacity(0.8f);
//        this.setBackground(Color.getHSBColor(216, 48, 90));
        this.setIconImages(FlatSVGUtils.createWindowIconImages("/icon/EasyTodo.svg"));
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
                x = e.getX();
                y = e.getY();
            }
        });
        JFrame mainframe = this;
        this.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                super.mouseDragged(e);
                Point point = mainframe.getLocation();
                int offsetx = e.getX()-x;
                int offsety = e.getY()-y;
                mainframe.setLocation(point.x+offsetx,point.y+offsety);
            }
        });
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                tray();
            }
        });
        if(SystemTray.isSupported()){
            // 创建弹出菜单
            SystemTray systemTray = SystemTray.getSystemTray();
            try {
                Image image2 = FlatSVGUtils.svg2image("/icon/EasyTodo.svg",1f);
                TrayIcon trayIcon = new TrayIcon(image2,"EasyTodo");
                trayIcon.setImageAutoSize(true);
                trayIcon.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(MouseEvent.BUTTON1 == e.getButton()){
                            mainframe.setVisible(!mainframe.isVisible());
                        }else if(MouseEvent.BUTTON3 == e.getButton()){
                            trayIcon.setPopupMenu(trayPopMenu);
                        }
                    }
                });
                systemTray.add(trayIcon);
                this.setVisible(false);
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    }

    @Autowired
    private TitlePane titlePane;
    @Autowired
    private MainPane mainPane;
    @Autowired
    private FootPane footPane;

    public void init(){

        GridBagLayout gridBagLayout = new GridBagLayout();
        this.setLayout(gridBagLayout);
        GridBagConstraints constraints = new GridBagConstraints();

        constraints.weightx = 1;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.weighty = 0.11;
        titlePane.setBackground(ColorTheme.BLUE);
        gridBagLayout.setConstraints(titlePane,constraints);
        constraints.weighty = 0.4;
        mainPane.setBackground(ColorTheme.BLUE);
        gridBagLayout.setConstraints(mainPane,constraints);
        constraints.weighty = 0.51;
        footPane.setBackground(ColorTheme.BLUE);
        gridBagLayout.setConstraints(footPane,constraints);

        this.add(titlePane);
        this.add(mainPane);
        this.add(footPane);

    }

    private void tray() {
        this.setVisible(false);
    }


}

主题 Theme

package com.mucong.easytodo.constant;

import java.awt.*;

public class ColorTheme {
    public static final Color BLUE = new Color(119,163,229);
    public static final Color ORANGE = new Color(249, 171, 98);

    public enum Switch{
        ON(0,"/icon/radio_on.png"),
        OFF(1,"/icon/radio_off.png");

        private int val;
        private String path;

        Switch(int val, String path) {
            this.val = val;
            this.path = path;
        }
        public static Switch getByVal(int val){
            for(Switch vl :Switch.values()){
                if(vl.val == val){
                    return vl;
                }
            }
            return null;
        }

        public int getVal() {
            return val;
        }

        public String getPath() {
            return path;
        }
    }
}

mainPane

package com.mucong.easytodo.ui.component;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.mucong.easytodo.constant.ColorTheme;
import com.mucong.easytodo.ui.dialog.TaskDialog;
import com.mucong.easytodo.util.SystemUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

@Component
public class MainPane extends JPanel {
    public JPanel accoutPanel;
    public JPanel taskBoard;
    public JPanel notesBoard;
    public JPanel histTask;
    public JPanel histNotes;

    public int bodeswitch = 0;
    public int noteswitch = 1;

    @Autowired
    private TaskDialog taskDialog;

    public MainPane() {
        accoutPanel = new noteBar(getLeft("/icon/accout.png", "木聪"), getright(null, null, 0, "")).init();
        taskBoard = new noteBar(getLeft("/icon/task.png", "任务版 "), getright("/icon/radio_off.png", null, bodeswitch, "task")).init();
        notesBoard = new noteBar(getLeft("/icon/notes.png", "便签板 "), getright("/icon/radio_on.png", null, noteswitch, "notes")).init();
        histTask = new noteBar(getLeft("/icon/histask.png", "历史任务"), getright(null, "25", 0, "")).init();
        histNotes = new noteBar(getLeft("/icon/hisnotes.png", "历史便签"), getright(null, "25", 0, "")).init();
        this.add(accoutPanel);
        this.add(createsep(0.9));
        this.add(taskBoard);
        this.add(createsep(0.9));
        this.add(notesBoard);
        this.add(createsep(0.9));
        this.add(histTask);
        this.add(createsep(0.9));
        this.add(histNotes);
        this.add(createsep(0.9));
        this.setBackground(ColorTheme.BLUE);
    }

    /**
     * 开关按钮添加事件
     */
    public class SwitchIcon extends JLabel {
        private int val;
        private String name;

        public SwitchIcon(int val, String name) {
            this.val = val;
            this.name = name;
        }

        public SwitchIcon init() {
            SwitchIcon icon = this;
            this.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    val = (val + 1) % 2;
                    showBoard(name);
                    ImageIcon closeIcon = new ImageIcon(getClass().getResource(ColorTheme.Switch.getByVal(val).getPath()));
                    closeIcon = new ImageIcon(closeIcon.getImage().getScaledInstance(28, 28, Image.SCALE_DEFAULT));
                    icon.setIcon(closeIcon);

                }
            });
            ImageIcon closeIcon = new ImageIcon(getClass().getResource(ColorTheme.Switch.getByVal(val).getPath()));
            closeIcon = new ImageIcon(closeIcon.getImage().getScaledInstance(28, 28, Image.SCALE_DEFAULT));
            icon.setIcon(closeIcon);
            return this;

        }

        private void showBoard(String name) {
            if (name.equals("task")) {
                taskDialog.setVisible((val == 0));
            }
        }
    }


    private JSeparator createsep(double v) {
        JSeparator separator = new JSeparator();
        separator.setPreferredSize(new Dimension((int) (SystemUtil.width * v), 2));
        separator.setBackground(Color.white);
        return separator;
    }


    public class noteBar extends JPanel {
        public JPanel left;
        public JPanel right;

        public noteBar(JPanel left, JPanel right) {
            this.left = left;
            this.right = right;
        }

        public noteBar init() {
            GridBagLayout gridBagLayout = new GridBagLayout();
            GridBagConstraints constraints = new GridBagConstraints();
            this.setLayout(gridBagLayout);
            constraints.weightx = 0.5;
            constraints.fill = GridBagConstraints.BOTH;
            constraints.weighty = 1;
            constraints.gridheight = GridBagConstraints.REMAINDER;

            left.setBackground(ColorTheme.BLUE);
            gridBagLayout.setConstraints(left, constraints);
            this.add(left);
            gridBagLayout.setConstraints(right, constraints);
            this.add(right);
            this.setPreferredSize(new Dimension((int) (SystemUtil.width * 0.91), 38));
            return this;
        }
    }

    private JPanel getright(String path, String text, int xx, String name) {
        JPanel right = new JPanel();
        if (path != null) {
            right.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
            right.add(new SwitchIcon(xx, name).init());
        } else if (text != null) {
            right.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 0));
            JLabel label = new JLabel(text);
            label.setForeground(ColorTheme.ORANGE);
            label.setFont(new Font("", 1, 18));
            right.add(label);
        }
        right.setBackground(ColorTheme.BLUE);
        return right;
    }

    private JPanel getLeft(String path, String text) {

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout(FlowLayout.LEFT));
        JLabel iconLabel = new JLabel();
        ImageIcon closeIcon = new ImageIcon(getClass().getResource(path));
        closeIcon = new ImageIcon(closeIcon.getImage().getScaledInstance(28, 28, Image.SCALE_DEFAULT));
        iconLabel.setIcon(closeIcon);
        panel.add(iconLabel);

        JLabel textLabel = new JLabel(text);
        textLabel.setForeground(Color.white);
        textLabel.setFont(new Font("", 1, 18));
        panel.add(textLabel);

        return panel;
    }

}

具体效果

调整页面非常的废时间,特别是布局组件的位置,因为我采用的是动态生成大小很多,组件的位置会和当前使用的屏幕的分辨率有关系。
在这里插入图片描述

🖼️使用环境

  • windows11
  • jdk1.8

🥷知识点

  • springboot启动非web项目
  • swing基础知识
  • h2数据库和sqlite选择
  • java打包成windows应用

spingboot启动swing

因为JFrame的构造方法会抛出一个HeadlessException 异常,修改springbootapplicationbuilder中的headless=false,web=none,然后后面直接执行自己的组件。然后通过ApplicationContext获取定义的对象就可以。如果是一般的程序,直接SpringbootApplication.run就行不需要设置参数。

//这里是源码里面的JFrame构造方法,会抛出异常直接springbootApplication.run会报错
public JFrame() throws HeadlessException {
        super();
        frameInit();
    }
package com.mucong;

import com.mucong.easytodo.EasytodoApp;
import org.springframework.beans.BeansException;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class App  {

    public static void main(String[] args) {
        try {
            SpringApplicationBuilder builder = new SpringApplicationBuilder(App.class);
            //设置headless=false,设置web为none
            ApplicationContext context = builder.headless(false).web(WebApplicationType.NONE).run(args);
            //获取对象然后正常执行
            EasytodoApp easytodoApp = context.getBean(EasytodoApp.class);
            easytodoApp.createUI();
        } catch (BeansException e) {
            e.printStackTrace();
        }
    }
}

总结

目前已经搭建了整体的框架,和基本的页面组件编写,接下来需要编写后续的功能实现,任务的添加和状态修改,便签的展示,桌面锁定(就是那个图钉的功能)

源码

目前代码已经开发完成了,可以在(二)查看效果。
项目地址 https://gitee.com/wmazh/easytodo

推荐

使用springboot搭建swing桌面程序(二)

  • 7
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Kotlin是一种静态类型的编程语言,具有JVM的可移植性和Java的互操作性。Spring Boot是一个用于创建独立的、基于Spring框架的Java应用程序的框架,它提供了快速开发应用程序所需的所有功能。JavaFX是一个用于创建丰富客户端应用程序的框架,它提供了丰富的UI组件和布局管理器。 要使用Kotlin Spring BootJavaFX开发桌面应用程序,需要完成以下步骤: 1. 创建一个Kotlin Spring Boot项目。可以使用Spring Initializr创建项目,选择Kotlin和Spring Web依赖项。 2. 添加JavaFX依赖项。可以在pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>16</version> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>16</version> </dependency> ``` 3. 创建一个JavaFX应用程序类。可以使用JavaFX的Application类作为应用程序的入口点。在这个类中,可以创建UI组件,处理事件和管理应用程序的状态。以下是一个简单的JavaFX应用程序类的示例: ```kotlin import javafx.application.Application import javafx.fxml.FXMLLoader import javafx.scene.Parent import javafx.scene.Scene import javafx.stage.Stage class MyApplication : Application() { override fun start(primaryStage: Stage?) { val root: Parent = FXMLLoader.load(javaClass.getResource("/fxml/main.fxml")) primaryStage?.title = "My Application" primaryStage?.scene = Scene(root) primaryStage?.show() } companion object { @JvmStatic fun main(args: Array<String>) { launch(MyApplication::class.java, *args) } } } ``` 4. 创建FXML布局文件。FXML是一种XML格式的文件,用于定义UI组件和布局。可以使用Scene Builder或手动创建FXML文件。以下是一个简单的FXML布局文件的示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane xmlns:fx="http://javafx.com/fxml/1" fx:id="root" prefHeight="400" prefWidth="600"> <Button fx:id="button" text="Click me" layoutX="250" layoutY="180" /> </AnchorPane> ``` 5. 在JavaFX应用程序类中加载FXML布局文件。可以使用FXMLLoader类加载FXML布局文件,并将其添加到应用程序的场景图中。以下是一个示例: ```kotlin val root: Parent = FXMLLoader.load(javaClass.getResource("/fxml/main.fxml")) primaryStage?.title = "My Application" primaryStage?.scene = Scene(root) primaryStage?.show() ``` 6. 处理UI事件。可以在JavaFX应用程序类中添加事件处理程序,以响应UI组件的事件。以下是一个处理按钮单击事件的示例: ```kotlin button.setOnAction { event -> println("Button clicked!") } ``` 7. 使用Spring Boot管理应用程序的状态。可以使用Spring Boot的依赖注入和管理功能来管理应用程序的状态和依赖关系。可以在Spring Boot的配置类中定义bean,然后在JavaFX应用程序类中使用它们。以下是一个简单的Spring Boot配置类的示例: ```kotlin @Configuration class AppConfig { @Bean fun myService(): MyService { return MyService() } } ``` 8. 在JavaFX应用程序类中使用Spring Boot的依赖注入功能。可以在JavaFX应用程序类的构造函数中注入Spring Boot管理的bean。以下是一个示例: ```kotlin class MyApplication : Application() { @Autowired lateinit var myService: MyService // ... } ``` 这就是使用Kotlin Spring BootJavaFX开发桌面应用程序的基本步骤。当然,还有很多其他的细节和技术,可以根据需要进行学习和应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值