当前位置: 首页 > news >正文

泉州专业网站建设常州百度推广公司

泉州专业网站建设,常州百度推广公司,广元网站建设价格,交互做的好网站背景 Springboot 在 Java 给 Java 开发带来了极大的便利,那么如何把它集成到 Swing GUI 编程项目中,使得 GUI 编程更加高效?本人简单做了一下尝试,完成一个 demo ,贴出来供大家参考 具体步骤 创建一个 spring boot …

背景

Springboot 在 Java 给 Java 开发带来了极大的便利,那么如何把它集成到 Swing GUI 编程项目中,使得 GUI 编程更加高效?本人简单做了一下尝试,完成一个 demo ,贴出来供大家参考

具体步骤

创建一个 spring boot 项目

这步就不需要多说了,大家参考 spring 官网的初始化工具,自行搞一个。

在 eclipse 中安装 Swing 设计工具

这步也相对比较简单,大家随便找个博客照抄就可以。

Spring 上下文工具类

这个有什么用呢?后面会讲,先上代码

package com.example.demo.context;import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;public class SpringContext implements ApplicationContextAware {private static ApplicationContext context = null;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {context = applicationContext;}public static ApplicationContext getContext() {return context;}
}

应用程序上下文工具类

同样,下文会看到怎么用,还是先上代码

package com.example.demo.context;import javax.swing.JFrame;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import com.example.demo.view.MainFrame;public class ApplicationContext implements ApplicationListener<ApplicationStartedEvent> {public ApplicationContext() {System.out.println("ApplicationContext is inited..");}private JFrame frame;@Overridepublic void onApplicationEvent(ApplicationStartedEvent event) {this.updateContent(MainFrame.getInstance());}public void updateContent(JFrame _frame) {if(this.frame != null) {this.frame.setVisible(false);}this.frame = _frame;this.frame.setVisible(true);}
}

写一个登录的 Frame

package com.example.demo.view;import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;import com.example.demo.context.ApplicationContext;
import com.example.demo.context.SpringContext;public class LoginFrame extends JFrame {private static final long serialVersionUID = 1L;private JPanel contentPane;private static LoginFrame instance;public static LoginFrame getInstance() {if(instance == null) {instance = new LoginFrame();}return instance;}/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {LoginFrame frame = new LoginFrame();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public LoginFrame() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);JButton btnNewButton = new JButton("New button");btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {SpringContext.getContext().getBean(ApplicationContext.class).updateContent(new MainFrame());}});contentPane.add(btnNewButton);}}

写一个主业务 Frame

package com.example.demo.view;import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;import com.example.demo.context.ApplicationContext;
import com.example.demo.context.SpringContext;public class MainFrame extends JFrame {private static final long serialVersionUID = 1L;private JPanel contentPane;private static MainFrame instance;public static MainFrame getInstance() {if(instance == null) {instance = new MainFrame();}return instance;}/*** Create the frame.*/protected MainFrame() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);JMenuBar menuBar = new JMenuBar();setJMenuBar(menuBar);JMenu mnNewMenu = new JMenu("系统");menuBar.add(mnNewMenu);JMenuItem mntmNewMenuItem = new JMenuItem("首页");mntmNewMenuItem.addActionListener(menuClick);mnNewMenu.add(mntmNewMenuItem);JMenuItem mntmNewMenuItem_1 = new JMenuItem("登录");mntmNewMenuItem_1.addActionListener(menuClick);mnNewMenu.add(mntmNewMenuItem_1);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(new BorderLayout(0, 0));}private void clearMainPanel() {Component[] cps = contentPane.getComponents();if(cps.length<=0) {return;}for(int i=0;i<cps.length;i++) {contentPane.remove(i);}}public static void main(String ...args) {EventQueue.invokeLater(new Runnable() {public void run() {try {MainFrame frame = new MainFrame();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}private ActionListener menuClick = new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println(e.getActionCommand());if("登录".equals(e.getActionCommand())) {SpringContext.getContext().getBean(ApplicationContext.class).updateContent(LoginFrame.getInstance());}}};
}

总结

  • demo 中实现了一个 SpringContext,使得在 Swing 中可以获取到 Spring 容器中的 bean
  • Frame 都用单例模式实现,大家可以考虑弄一个基类来继承,通过设置 Frame 是否可见来实现 Frame 直接的切换

后面找点时间做一个有用的客户端。欢迎大家交流。

http://www.ds6.com.cn/news/19148.html

相关文章:

  • 找代做海报的网站网络推广网站的方法
  • 短链接生成算法外链seo推广
  • 做网站前台后台是怎么连接的免费建站哪个网站最好
  • 网站备案填了虚假座机能过吗宁波seo网络优化公司
  • 网站的页面设计网盟推广平台
  • 宜昌网站开发公司百度竞价排名官网
  • 电脑版浏览器入口seo全网图文推广
  • ASP.NET动态网站开发技术实用教程企业文化培训
  • 工业产品设计工资seo文案范例
  • 网站建设 行业资讯如何做外贸网站的推广
  • 手工制作大全女生的最爱上海seo外包
  • 亚马逊deal网站怎么做百度号码认证平台首页
  • 个人工作室网站百度投诉电话
  • 什么是 网站的逻辑结构企业seo关键字优化
  • 雄安个人代做网站排名网络营销运营
  • 网站后台怎么改密码搜狗收录
  • 网站上做时时彩代理赚钱吗营销推广案例
  • 小伙反串做直播视频网站电商网站建设公司哪家好
  • 做网站完整过程seo营销网站
  • 有域名后怎么做网站360指数查询
  • 厦门网站建设 软件园网站seo推广排名
  • 中国铁路建设工程招标网站seo排名优化培训价格
  • 商标logo图案大全图片seo技术是什么意思
  • 苹果手机打开flash网站源码产品推广策略怎么写
  • 做网站注意事项阿里巴巴国际站官网
  • 网站互动栏目设置宁波的网络营销服务公司
  • 网站收录了怎么做排名聊城网站推广的公司
  • 网站 关键词关键词挖掘查询工具
  • 摄影工作室logo设计哈尔滨seo优化软件
  • 萍乡海绵城市建设官方网站怎么优化网站