1、下载内嵌浏览器Jar包
下载地址:点击下载
2、项目下加入对应jar;然后右键:Add as Library...
3、添加启动项目后事件
import com.teamdev.jxbrowser.chromium.Browser; import com.teamdev.jxbrowser.chromium.swing.BrowserView; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import javax.swing.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** * @author 鲁达 * createTime 2019-12-29 20:51 **/ @Component public class ApplicationRunnerImpl implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { String url = "http://localhost:9028/open/index"; JFrame frame = new JFrame(); // 谷歌内核浏览器 Browser browser = new Browser(); BrowserView view = new BrowserView(browser); //禁用close功能 // frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); //隐藏任务栏图标 // frame.setType(JFrame.Type.UTILITY); // //不显示标题栏,最大化,最小化,退出按钮 frame.setUndecorated(true); //尺寸 // frame.setSize(500,500); //坐标 frame.setLocation(0,0); frame.add(view); //全屏显示 frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // 是否显示 frame.setVisible(true); //是否在屏幕最上层显示 frame.setAlwaysOnTop(true); //加载地址 browser.loadURL(url); // System.out.println(frame.getX()); // System.out.println(frame.getY()); // list.add(frame); frame.addWindowListener(new WindowAdapter() { // 窗口关闭时间监听 @Override public void windowClosing(WindowEvent e){ System.out.println("窗口关闭..."); } }); } }
4、然后在ide 的run--->VM Options里加上一句-Djava.awt.headless=false
解决问题:java.awt.HeadlessException运行时异常
解决问题:打包找不到依赖的问题
<!--内嵌浏览器--> <dependency> <groupId>jxbrowser-6.14</groupId> <artifactId>jxbrowser-6.14</artifactId> <version>6.14</version> <scope>system</scope> <systemPath>${project.basedir}\src\main\resources\lib\jxbrowser-6.14.jar</systemPath> </dependency> <dependency> <groupId>jxbrowser-win32-6.14</groupId> <artifactId>jxbrowser-win32-6.14</artifactId> <version>6.14</version> <scope>system</scope> <systemPath>${project.basedir}\src\main\resources\lib\jxbrowser-win32-6.14.jar</systemPath> </dependency>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <includeSystemScope>true</includeSystemScope> </configuration> </plugin> </plugins> </build>