前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Springboot如何启动图形界面程序以及如何多开

Springboot如何启动图形界面程序以及如何多开

作者头像
算法之名
发布2019-08-20 11:05:26
2.5K0
发布2019-08-20 11:05:26
举报
文章被收录于专栏:算法之名算法之名

虽然很少有人用java写windows界面,但比如说我们有一个图形界面程序,是用java.awt写的,大概是这样的吧

代码语言:javascript
复制
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Font;

@Component
public class Swingclient extends JFrame implements ActionListener {
   
   @Autowired
   private Client client;
   
   /**
    * 
    */
   private static final long serialVersionUID = 2572235358190956651L;
   
   /**
    * 玩家信息
    */
   private PlayerResponse playerResponse;
   
   /**
    * 用户名
    */
   private JTextField playerName;
   
   /**
    * 密码
    */
   private JTextField passward;
   
   /**
    * 登录按钮
    */
   private JButton loginButton;
   
   
   /**
    * 注册按钮
    */
   private JButton register;

   /**
    * 聊天内容
    */
   private JTextArea chatContext;
   
   /**
    * 发送内容
    */
   private JTextField message;
   
   /**
    * 目标用户
    */
   private JTextField targetPlayer;
   
   /**
    * 发送按钮
    */
   private JButton sendButton;

   /**
    * 操作提示
    */
   private JLabel tips;

   

   public Swingclient() {
      
      getContentPane().setLayout(null);
      
      //登录部分
      JLabel lblIp = new JLabel("角色名");
      lblIp.setFont(new Font("宋体", Font.PLAIN, 12));
      lblIp.setBounds(76, 40, 54, 15);
      getContentPane().add(lblIp);
      
      playerName = new JTextField();
      playerName.setBounds(139, 37, 154, 21);
      getContentPane().add(playerName);
      playerName.setColumns(10);
      
      JLabel label = new JLabel("密  码");
      label.setFont(new Font("宋体", Font.PLAIN, 12));
      label.setBounds(76, 71, 54, 15);
      getContentPane().add(label);
      
      passward = new JTextField();
      passward.setColumns(10);
      passward.setBounds(139, 68, 154, 21);
      getContentPane().add(passward);
      
      //登录
      loginButton = new JButton("登录");
      loginButton.setFont(new Font("宋体", Font.PLAIN, 12));
      loginButton.setActionCommand(ButtonCommand.LOGIN);
      loginButton.addActionListener(this);
      loginButton.setBounds(315, 37, 93, 23);
      getContentPane().add(loginButton);
      
      //注册
      register = new JButton("注册");
      register.setFont(new Font("宋体", Font.PLAIN, 12));
      register.setActionCommand(ButtonCommand.REGISTER);
      register.addActionListener(this);
      register.setBounds(315, 67, 93, 23);
      getContentPane().add(register);
      
      //聊天内容框
      chatContext = new JTextArea();
      chatContext.setLineWrap(true);
      
      JScrollPane scrollBar = new JScrollPane(chatContext);
      scrollBar.setBounds(76, 96, 93, 403);
      scrollBar.setSize(336, 300);
      getContentPane().add(scrollBar);

      
      //发送部分
      JLabel label_7 = new JLabel("消息");
      label_7.setFont(new Font("宋体", Font.PLAIN, 12));
      label_7.setBounds(76, 411, 54, 15);
      getContentPane().add(label_7);
      
      message = new JTextField();
      message.setBounds(139, 408, 222, 21);
      getContentPane().add(message);
      message.setColumns(10);
      
      JLabel lblid = new JLabel("角色");
      lblid.setFont(new Font("宋体", Font.PLAIN, 12));
      lblid.setBounds(76, 436, 43, 24);
      getContentPane().add(lblid);

      targetPlayer = new JTextField();
      targetPlayer.setBounds(139, 438, 133, 21);
      getContentPane().add(targetPlayer);
      targetPlayer.setColumns(10);
      
      sendButton = new JButton("发送");
      sendButton.setFont(new Font("宋体", Font.PLAIN, 12));
      sendButton.setBounds(382, 407, 67, 23);
      sendButton.setActionCommand(ButtonCommand.SEND);
      sendButton.addActionListener(this);
      getContentPane().add(sendButton);
      
      //错误提示区域
      tips = new JLabel();
      tips.setForeground(Color.red);
      tips.setFont(new Font("宋体", Font.PLAIN, 14));
      tips.setBounds(76, 488, 200, 15);
      getContentPane().add(tips);
      

      int weigh = 500;
      int heigh = 600;
      int w = (Toolkit.getDefaultToolkit().getScreenSize().width - weigh) / 2;
      int h = (Toolkit.getDefaultToolkit().getScreenSize().height - heigh) / 2;
      this.setLocation(w, h);
      this.setTitle("聊天工具");
      this.setSize(weigh, heigh);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setResizable(true);
   }

(以上代码不全,请勿使用。)

这样的程序在Springboot里面是肯定启动不了的,会报一个java.awt.HeadlessException的异常,为了让springboot能够使用,我们会把springboot的main方法改造一下,代码如下

代码语言:javascript
复制
package com.guanjian;

import com.guanjian.client.swing.Swingclient;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan({"com.guanjian"})
@EnableAutoConfiguration
@SpringBootApplication
public class SpringnettyApplication {

   public static void main(String[] args) {
      SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringnettyApplication.class);
      ApplicationContext applicationContext = builder.headless(false).run(args);
      Swingclient swing = applicationContext.getBean(Swingclient.class);
      swing.setVisible(true);
   }
}

重点是builder.headless(false)这里,这样就可以把Java写的窗口运行起来,另外Springboot一般都是单实例运行,多开的方法如下

把右上角的Single instance only的勾去掉就可以了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档