自学内容网 自学内容网

Springboot 集成 Swing

背景

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;
@Override
public 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;
@Override
public 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() {
@Override
public 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 直接的切换

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


原文地址:https://blog.csdn.net/luyangbin01/article/details/142265578

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!