🎈概述
在习惯使用springboot开发代码之后,在我们自己进行桌面程序编码的时候,发现很多的不方便,比如连接数据库,读取配置文件,循环依赖,日志记录,定时任务等等。这篇文章详细介绍了使用springboot搭建一个桌面程序easytodo,一个桌面便签和任务,使用jpa+h2作为本地存储,打包成windows应用。
文章目录
- 🎈概述
- 🤖项目介绍
- 🦜UI设计
- 🐎主要代码
- mainFrame
- 主题 Theme
- mainPane
- 具体效果
- 🖼️使用环境
- 🥷知识点
- spingboot启动swing
- 总结
- 源码
- 推荐
🤖项目介绍
- 支持快速添加任务
- 快速完成状态更新
- 任务支持分类
- 支持统计
- 桌面展示
- 静态展示,单一颜色,防止审美疲劳
🦜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