由于在springboot项目中要加入启动等候的功能,即在项目在加载启动的时候,要在桌面上弹出自定义的画面,画面下面要有进度条
1.跟Application同一个地方加入文件JWindows.java
import com.CIDataCompare.application.controller.LoginController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.net.*;
//程序启动界面
public class JWindowDemo extends JWindow implements Runnable {
Thread splashThread; //进度条更新线程
JProgressBar progress; //进度条
public volatile boolean exit = false;
private Logger logger= LoggerFactory.getLogger(this.getClass());
public JWindowDemo() {
Container container=getContentPane(); //得到容器
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); //设置光标
String url1=getClass().getResource("").getPath();
logger.info("=================="+url1);
URL url = getClass().getResource("login.png"); //图片的位置
if(url != null){
container.add(new JLabel(new ImageIcon(url)),BorderLayout.CENTER); //增加图片
}
progress = new JProgressBar(1,500); //实例化进度条
progress.setStringPainted(true); //描绘文字
progress.setString("Loading......."); //设置显示文字
progress.setBackground(Color.white); //设置背景色
container.add(progress,BorderLayout.SOUTH); //增加进度条到容器上
Dimension screen = getToolkit().getScreenSize(); //得到屏幕尺寸
pack(); //窗口适应组件尺寸
setLocation((screen.width-getSize().width)/2,(screen.height-getSize().height)/2); //设置窗口位置
splashThread=new Thread(this); //实例化线程
splashThread.start(); //开始运行线程
}
public void start(){
this.toFront(); //窗口前端显示
/*splashThread=new Thread(this); //实例化线程
splashThread.start(); //开始运行线程*/
}
public void run(){
setVisible(true); //显示窗口
try {
for (int i=0;i<1000;i++){
if(splashThread.interrupted()){
break;
}else{
splashThread.sleep(150); //线程休眠
if(Application.num==100){
progress.setValue(500); //设置进度条值
splashThread.sleep(5000); //线程休眠
dispose(); //释放窗口
exit=true;
splashThread.interrupt();
System.out.println("====111===="+Thread.currentThread().getName());
Thread.currentThread().interrupt();
break;
}else{
progress.setValue(progress.getValue()+1); //设置进度条值
logger.info(progress.getValue()+1+"");
}
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
dispose(); //释放窗口
splashThread.interrupted();
}
2.在Application.java中添加启动接口,添加主线程启动标志num=100
package com.CIDataCompare.application;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import javax.swing.*;
import java.awt.*;
import java.io.File;
@SpringBootApplication
public class Application {
public static int num=0;
public static void main(String[] args) {
JWindowDemo splash = new JWindowDemo();
//splash.start(); //运行启动界面
//SpringApplication.run(Application.class, args);
SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
builder.headless(false).web(true).run(args);
try{
File directory = new File("");
String mydir = directory.getAbsolutePath();
//用谷歌浏览器打开
System.setProperty("webdriver.chrome.driver", mydir+"\\chromedriver.exe");
ChromeOptions options=new ChromeOptions();
options.addArguments("disable-infobars");
//实例化webdriver的对象,启动谷歌浏览器
WebDriver driver = new ChromeDriver(options);
//通过对象driver调用具体的get方法来打开网页
driver.get("http://localhost:60032/");
}catch (Exception e){
//用默认浏览器打开
try {
//String url = "http://www.baidu.com";
String url = "http://localhost:60032";
java.net.URI uri = java.net.URI.create(url);
// 获取当前系统桌面扩展
Desktop dp =Desktop.getDesktop();
// 判断系统桌面是否支持要执行的功能
if (dp.isSupported(java.awt.Desktop.Action.BROWSE)) {
//File file = new File("D:\\aa.txt");
//dp.edit(file);// 编辑文件
dp.browse(uri);// 获取系统默认浏览器打开链接
// dp.open(file);// 用默认方式打开文件
// dp.print(file);// 用打印机打印文件
}
} catch (java.lang.NullPointerException e1) {
// 此为uri为空时抛出异常
e.printStackTrace();
} catch (java.io.IOException e2) {
// 此为无法获取系统默认浏览器
e.printStackTrace();
}
}
System.out.println("===========Application=========================");
num=100;
}
}
3.将1中URL url = getClass().getResource("login.png"); //图片的位置
中所要加载的图片放到
String url1=getClass().getResource("").getPath();
logger.info("=================="+url1);
打印的地址下面 ,即项目打包后的 \target\classes\com\CIDataCompare\application
下面
4.启动项目
5.启动之后出现的一个问题 ,for循环的时候,总是有两个线程在循环,当主线程启动之后,想要退出启动画面的线程退不出去启动打印的log
2019-08-12 13:27:55.979 [main] INFO com.CIDataCompare.application.JWindowDemo - ==================/E:/workspace/CIDVS/target/classes/com/CIDataCompare/application/
2019-08-12 13:27:56.211 [restartedMain] INFO com.CIDataCompare.application.JWindowDemo - ==================/E:/workspace/CIDVS/target/classes/com/CIDataCompare/application/
2019-08-12 13:27:56.728 [Thread-4] INFO com.CIDataCompare.application.JWindowDemo - 6
2019-08-12 13:27:56.866 [Thread-8] INFO com.CIDataCompare.application.JWindowDemo - 6
2019-08-12 13:27:56.881 [Thread-4] INFO com.CIDataCompare.application.JWindowDemo - 7
2019-08-12 13:27:57.028 [Thread-8] INFO com.CIDataCompare.application.JWindowDemo - 7
后来查才知道是pom.xml中加了热部署的依赖会导致这个问题
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
把这个注释掉之后就可以了