Springboot如何啓動圖形界面程序以及如何多開

雖然不多有人用java寫windows界面,但好比說咱們有一個圖形界面程序,是用java.awt寫的,大概是這樣的吧java

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);
   }

(以上代碼不全,請勿使用。)spring

這樣的程序在Springboot裏面是確定啓動不了的,會報一個java.awt.HeadlessException的異常,爲了讓springboot可以使用,咱們會把springboot的main方法改造一下,代碼以下windows

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通常都是單實例運行,多開的方法以下springboot

把右上角的Single instance only的勾去掉就能夠了。app

相關文章
相關標籤/搜索