🎈概述
在习惯使用springboot开发代码之后,在我们自己进行桌面程序编码的时候,发现很多的不方便,比如连接数据库,读取配置文件,循环依赖,日志记录,定时任务等等。这篇文章详细介绍了使用springboot搭建一个桌面程序easytodo,一个桌面便签和任务,使用jpa+h2作为本地存储,打包成windows应用。
文章目录
🤖项目介绍
- 支持快速添加任务
- 快速完成状态更新
- 任务支持分类
- 支持统计
- 桌面展示
- 静态展示,单一颜色,防止审美疲劳
🦜UI设计
使用figma工具设计的,具体页面地址 easytodo页面设计,设计要求是颜色冷淡为主,主要颜色是淡蓝色和白色,用橙色作为点缀😏。设计了三个logo,个人比较喜欢第一个,可惜已经别人用了,最后采用的第三个,我没有去抄袭别人的图标,确实就是按照功能直接设计出来的,圆形打个勾勒表示任务完成。
🐎主要代码
废话不多说直接上代码😎
mainFrame
packagecom.mucong.easytodo.ui;importcom.formdev.flatlaf.extras.FlatSVGUtils;importcom.mucong.easytodo.constant.ColorTheme;importcom.mucong.easytodo.ui.component.FootPane;importcom.mucong.easytodo.ui.component.MainPane;importcom.mucong.easytodo.ui.component.TitlePane;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;@ComponentpublicclassMainFrameextendsJFrame{privateint x;privateint y;publicint width;publicint heght;publicint dpi;@AutowiredprivateTrayPopMenu trayPopMenu;publicMainFrame()throwsHeadlessException{//计算窗口起始大小和位置,中间位置,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(newMouseAdapter(){@OverridepublicvoidmousePressed(MouseEvent e){super.mousePressed(e);
x = e.getX();
y = e.getY();}});JFrame mainframe =this;this.addMouseMotionListener(newMouseMotionAdapter(){@OverridepublicvoidmouseDragged(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(newWindowAdapter(){@OverridepublicvoidwindowClosing(WindowEvent e){tray();}});if(SystemTray.isSupported()){// 创建弹出菜单SystemTray systemTray =SystemTray.getSystemTray();try{Image image2 =FlatSVGUtils.svg2image("/icon/EasyTodo.svg",1f);TrayIcon trayIcon =newTrayIcon(image2,"EasyTodo");
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(newMouseAdapter(){@OverridepublicvoidmouseClicked(MouseEvent e){if(MouseEvent.BUTTON1 == e.getButton()){
mainframe.setVisible(!mainframe.isVisible());}elseif(MouseEvent.BUTTON3 == e.getButton()){
trayIcon.setPopupMenu(trayPopMenu);}}});
systemTray.add(trayIcon);this.setVisible(false);}catch(AWTException e){
e.printStackTrace();}}}@AutowiredprivateTitlePane titlePane;@AutowiredprivateMainPane mainPane;@AutowiredprivateFootPane footPane;publicvoidinit(){GridBagLayout gridBagLayout =newGridBagLayout();this.setLayout(gridBagLayout);GridBagConstraints constraints =newGridBagConstraints();
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);}privatevoidtray(){this.setVisible(false);}}
主题 Theme
packagecom.mucong.easytodo.constant;importjava.awt.*;publicclassColorTheme{publicstaticfinalColor BLUE =newColor(119,163,229);publicstaticfinalColor ORANGE =newColor(249,171,98);publicenumSwitch{ON(0,"/icon/radio_on.png"),OFF(1,"/icon/radio_off.png");privateint val;privateString path;Switch(int val,String path){this.val = val;this.path = path;}publicstaticSwitchgetByVal(int val){for(Switch vl :Switch.values()){if(vl.val == val){return vl;}}returnnull;}publicintgetVal(){return val;}publicStringgetPath(){return path;}}}
mainPane
packagecom.mucong.easytodo.ui.component;importcom.intellij.uiDesigner.core.GridConstraints;importcom.intellij.uiDesigner.core.GridLayoutManager;importcom.mucong.easytodo.constant.ColorTheme;importcom.mucong.easytodo.ui.dialog.TaskDialog;importcom.mucong.easytodo.util.SystemUtil;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importjavax.swing.*;importjava.awt.*;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;@ComponentpublicclassMainPaneextendsJPanel{publicJPanel accoutPanel;publicJPanel taskBoard;publicJPanel notesBoard;publicJPanel histTask;publicJPanel histNotes;publicint bodeswitch =0;publicint noteswitch =1;@AutowiredprivateTaskDialog taskDialog;publicMainPane(){
accoutPanel =newnoteBar(getLeft("/icon/accout.png","木聪"),getright(null,null,0,"")).init();
taskBoard =newnoteBar(getLeft("/icon/task.png","任务版 "),getright("/icon/radio_off.png",null, bodeswitch,"task")).init();
notesBoard =newnoteBar(getLeft("/icon/notes.png","便签板 "),getright("/icon/radio_on.png",null, noteswitch,"notes")).init();
histTask =newnoteBar(getLeft("/icon/histask.png","历史任务"),getright(null,"25",0,"")).init();
histNotes =newnoteBar(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);}/**
* 开关按钮添加事件
*/publicclassSwitchIconextendsJLabel{privateint val;privateString name;publicSwitchIcon(int val,String name){this.val = val;this.name = name;}publicSwitchIconinit(){SwitchIcon icon =this;this.addMouseListener(newMouseAdapter(){@OverridepublicvoidmouseClicked(MouseEvent e){
val =(val +1)%2;showBoard(name);ImageIcon closeIcon =newImageIcon(getClass().getResource(ColorTheme.Switch.getByVal(val).getPath()));
closeIcon =newImageIcon(closeIcon.getImage().getScaledInstance(28,28,Image.SCALE_DEFAULT));
icon.setIcon(closeIcon);}});ImageIcon closeIcon =newImageIcon(getClass().getResource(ColorTheme.Switch.getByVal(val).getPath()));
closeIcon =newImageIcon(closeIcon.getImage().getScaledInstance(28,28,Image.SCALE_DEFAULT));
icon.setIcon(closeIcon);returnthis;}privatevoidshowBoard(String name){if(name.equals("task")){
taskDialog.setVisible((val ==0));}}}privateJSeparatorcreatesep(double v){JSeparator separator =newJSeparator();
separator.setPreferredSize(newDimension((int)(SystemUtil.width * v),2));
separator.setBackground(Color.white);return separator;}publicclass noteBar extendsJPanel{publicJPanel left;publicJPanel right;publicnoteBar(JPanel left,JPanel right){this.left = left;this.right = right;}public noteBar init(){GridBagLayout gridBagLayout =newGridBagLayout();GridBagConstraints constraints =newGridBagConstraints();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(newDimension((int)(SystemUtil.width *0.91),38));returnthis;}}privateJPanelgetright(String path,String text,int xx,String name){JPanel right =newJPanel();if(path !=null){
right.setLayout(newFlowLayout(FlowLayout.RIGHT,0,0));
right.add(newSwitchIcon(xx, name).init());}elseif(text !=null){
right.setLayout(newFlowLayout(FlowLayout.RIGHT,5,0));JLabel label =newJLabel(text);
label.setForeground(ColorTheme.ORANGE);
label.setFont(newFont("",1,18));
right.add(label);}
right.setBackground(ColorTheme.BLUE);return right;}privateJPanelgetLeft(String path,String text){JPanel panel =newJPanel();
panel.setLayout(newFlowLayout(FlowLayout.LEFT));JLabel iconLabel =newJLabel();ImageIcon closeIcon =newImageIcon(getClass().getResource(path));
closeIcon =newImageIcon(closeIcon.getImage().getScaledInstance(28,28,Image.SCALE_DEFAULT));
iconLabel.setIcon(closeIcon);
panel.add(iconLabel);JLabel textLabel =newJLabel(text);
textLabel.setForeground(Color.white);
textLabel.setFont(newFont("",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会报错publicJFrame()throwsHeadlessException{super();frameInit();}
packagecom.mucong;importcom.mucong.easytodo.EasytodoApp;importorg.springframework.beans.BeansException;importorg.springframework.boot.WebApplicationType;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.boot.builder.SpringApplicationBuilder;importorg.springframework.context.ApplicationContext;@SpringBootApplicationpublicclassApp{publicstaticvoidmain(String[] args){try{SpringApplicationBuilder builder =newSpringApplicationBuilder(App.class);//设置headless=false,设置web为noneApplicationContext 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桌面程序(二)
版权归原作者 木聪 所有, 如有侵权,请联系我们删除。