From c20b95e43909e5071ad8e7c464d1e8f49f93d062 Mon Sep 17 00:00:00 2001 From: wangbing Date: Mon, 9 Oct 2023 17:16:06 +0800 Subject: [PATCH] init --- .gitignore | 8 + pom.xml | 187 ++++++++++++++++ .../java/com/example/jcef/Application.java | 21 ++ src/main/java/com/example/jcef/Demo.java | 53 +++++ src/main/java/com/example/jcef/KeyEnum.java | 58 +++++ src/main/java/com/example/jcef/WebClient.java | 203 ++++++++++++++++++ 6 files changed, 530 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/example/jcef/Application.java create mode 100644 src/main/java/com/example/jcef/Demo.java create mode 100644 src/main/java/com/example/jcef/KeyEnum.java create mode 100644 src/main/java/com/example/jcef/WebClient.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e5dba25 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +*.iml +.idea/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b2e98bc --- /dev/null +++ b/pom.xml @@ -0,0 +1,187 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.7.RELEASE + + + com.example + starter-jcef + 0.0.1-SNAPSHOT + jar + + starter-jcef + project for Spring Boot + + + UTF-8 + UTF-8 + 1.8 + true + 2.3.7.RELEASE + Hoxton.SR8 + + + + + + central + Central Repository + default + https://maven.aliyun.com/repository/public + + + + + + central + Central Repository + https://maven.aliyun.com/repository/public + default + + + + + + org.springframework.boot + spring-boot-starter-web + + + + me.friwi + jcefmaven + 110.0.25.1 + + + + me.friwi + jcef-natives-windows-amd64 + jcef-87476e9+cef-110.0.25+g75b1c96+chromium-110.0.5481.78 + + + + + + + + + + + + + + + + + com.melloware + jintellitype + 1.4.0 + + + + org.springframework.boot + spring-boot-devtools + provided + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + ${artifactId}-${version} + + src/main/java + + src/test/java + + + src/main/resources + + + + src/main/java + + **/*Mapper.xml + + + + + ${basedir}/src/main/resources/lib + BOOT-INF/lib/ + + *.jar + + + + + ${basedir}/src/main/resources/部署文档 + + *.bat + *.sh + + ${basedir}/target/部署文档 + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + + + + + org.apache.maven.plugins + maven-war-plugin + + + + ${project.basedir}/src/main/resources/lib + WEB-INF/lib + + *.jar + + + + + + + + diff --git a/src/main/java/com/example/jcef/Application.java b/src/main/java/com/example/jcef/Application.java new file mode 100644 index 0000000..ca3bbf2 --- /dev/null +++ b/src/main/java/com/example/jcef/Application.java @@ -0,0 +1,21 @@ +package com.example.jcef; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +@SpringBootApplication +public class Application extends SpringBootServletInitializer { + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(Application.class); + } + + public static void main(String[] args) { + SpringApplication application = new SpringApplication(Application.class); + application.run(args); + } + +} diff --git a/src/main/java/com/example/jcef/Demo.java b/src/main/java/com/example/jcef/Demo.java new file mode 100644 index 0000000..daf0c53 --- /dev/null +++ b/src/main/java/com/example/jcef/Demo.java @@ -0,0 +1,53 @@ +package com.example.jcef; + +import me.friwi.jcefmaven.CefInitializationException; +import me.friwi.jcefmaven.UnsupportedPlatformException; + +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.io.IOException; + +public class Demo { + + public static void main(String[] args) throws InterruptedException, UnsupportedPlatformException, CefInitializationException, IOException { + WebClient webClient = new WebClient(); + + // 焦点事件监听 + webClient.addFocusListener(new FocusListener() { + @Override + public void focusGained(FocusEvent e) { + System.out.println("获得焦点事件"); + } + + @Override + public void focusLost(FocusEvent e) { + System.out.println("丢失焦点事件"); + } + }); + + // 添加一个鼠标点击监听 + webClient.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + System.out.println("鼠标点击事件"); + } + }); + + // 添加一个鼠标点击监听 + webClient.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + System.out.println("鼠标点击事件"); + } + }); + + // 调用脚本 + webClient.executeJavaScript("console.log('java 调用 JavaScript')"); + + // 打开地址 + webClient.loadUrl("D:\\wbProgram\\nginx-1.23.0\\html\\index.html"); +// webClient.loadUrl("http://10.10.10.23:8082/#/index?token=fd290104-1568-4c65-87eb-14aeb95aee59"); + } +} diff --git a/src/main/java/com/example/jcef/KeyEnum.java b/src/main/java/com/example/jcef/KeyEnum.java new file mode 100644 index 0000000..930d92a --- /dev/null +++ b/src/main/java/com/example/jcef/KeyEnum.java @@ -0,0 +1,58 @@ +package com.example.jcef; + +public enum KeyEnum { + + F1(112), + F2(113), + F3(114), + F4(115), + F5(116), + F6(117), + F7(118), + F8(119), + F9(120), + F10(121), + F11(122), + F12(123), + ESC(27), + TAB(9), + CAPSLOCK(20), + SHIFT(16), + CTRL(17), + START_LEFT(91), + START_RIGHT(92), + CONTEXT_MENU(93), + ALT(18), + SPACE(32), + CARRIAGE_RETURN(13), + LINE_FEED(10), + BACK_SLASH(220), + BACK_SPACE(8), + INSERT(45), + DEL(46), + HOME(36), + END(35), + PAGE_UP(33), + PAGE_DOWN(34), + PRINT_SCREEN(44), + SCR_LK(145), + PAUSE(19), + LEFT_ARROW_KEY(37), + UP_ARROW_KEY(38), + RIGHT_ARROW_KEY(39), + DOWN_ARROW_KEY(40), + MOD_ALT(1), + MOD_CONTROL(2), + MOD_SHIFT(4), + MOD_WIN(8); + + private int value; + + KeyEnum(int value) { + this.value = value; + } + + public int value() { + return value; + } +} diff --git a/src/main/java/com/example/jcef/WebClient.java b/src/main/java/com/example/jcef/WebClient.java new file mode 100644 index 0000000..a71f408 --- /dev/null +++ b/src/main/java/com/example/jcef/WebClient.java @@ -0,0 +1,203 @@ +package com.example.jcef; + +import com.melloware.jintellitype.HotkeyListener; +import com.melloware.jintellitype.JIntellitype; +import me.friwi.jcefmaven.CefAppBuilder; +import me.friwi.jcefmaven.CefBuildInfo; +import me.friwi.jcefmaven.CefInitializationException; +import me.friwi.jcefmaven.MavenCefAppHandlerAdapter; +import me.friwi.jcefmaven.UnsupportedPlatformException; +import org.cef.CefApp; +import org.cef.CefClient; +import org.cef.browser.CefBrowser; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.FocusListener; +import java.awt.event.KeyListener; +import java.awt.event.MouseListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.IOException; + +public class WebClient extends JFrame { + + // 退出 + private final int KEY_MARK_ESC = 1; + // 最小化 + private final int KEY_MARK_SWITCH = 2; + // 刷新 + private final int KEY_MARK_RELOAD = 3; + // 强制刷新 + private final int KEY_MARK_RELOAD_FORCE = 4; + + private CefClient cefClient; + private CefBrowser cefBrowser; + + private final HotkeyListener hotkeyListener = new HotkeyListener() { + @Override + public void onHotKey(int i) { + switch (i) { + case KEY_MARK_ESC: { + System.out.println("log: esc"); + JIntellitype.getInstance().unregisterHotKey(KEY_MARK_ESC); + JIntellitype.getInstance().removeHotKeyListener(hotkeyListener); + close(); + } + break; + case KEY_MARK_SWITCH: { + System.out.println("log: switch"); + if (getExtendedState() == Frame.NORMAL) { + removeNotify(); + setUndecorated(true); + setExtendedState(JFrame.MAXIMIZED_BOTH); + } else { + setExtendedState(Frame.NORMAL); + removeNotify(); + setUndecorated(false); + } + setVisible(true); + } + break; + case KEY_MARK_RELOAD: { + System.out.println("log: reload"); + reload(); + } + break; + case KEY_MARK_RELOAD_FORCE: { + System.out.println("log: reloadForce"); + reloadForce(); + } + break; + } + } + }; + + public WebClient() throws HeadlessException, InterruptedException, UnsupportedPlatformException, CefInitializationException, IOException { + super(); + initCef(new String[]{}); + initClient(new String[]{}); + } + + private void initClient(String[] args) { + setTitle("WebClient"); + getContentPane().add(cefBrowser.getUIComponent(), BorderLayout.CENTER); + setSize(800, 600);//初始大小 + setLocation(0, 0);//位置 + // 设置为全屏模式 + setExtendedState(JFrame.MAXIMIZED_BOTH); + setUndecorated(true); + setAlwaysOnTop(true); + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + // 关闭应用时要释放资源 + CefApp.getInstance().dispose(); + dispose(); + System.exit(0);//0正常退出,1非正常退出 + } + }); + + // 添加热键 + JIntellitype.getInstance().registerHotKey(KEY_MARK_ESC, 0, KeyEnum.ESC.value()); + JIntellitype.getInstance().registerHotKey(KEY_MARK_SWITCH, 0, KeyEnum.F11.value()); + JIntellitype.getInstance().registerHotKey(KEY_MARK_RELOAD, 0, KeyEnum.F5.value()); + JIntellitype.getInstance().registerHotKey(KEY_MARK_RELOAD_FORCE, KeyEnum.MOD_CONTROL.value(), KeyEnum.F5.value()); + JIntellitype.getInstance().addHotKeyListener(hotkeyListener); + + // 显示 + setVisible(true); + } + + /** + * 初始化 CefBrowser + * + * @param args + */ + private void initCef(String[] args) throws InterruptedException, UnsupportedPlatformException, CefInitializationException, IOException { + CefAppBuilder builder = new CefAppBuilder(); + // window下不需要OSR + boolean useOSR = false; + builder.getCefSettings().windowless_rendering_enabled = useOSR; + builder.setAppHandler(new MavenCefAppHandlerAdapter() { + @Override + public void stateHasChanged(org.cef.CefApp.CefAppState state) { + // 关闭应用时退出jvm运行 + if (state == CefApp.CefAppState.TERMINATED) System.exit(0); + } + }); + + // 设置cef运行参数,这里为空 + builder.addJcefArgs(args); + + // 这边不设置,程序会自动解压 + // 设置 cef chrome实例的目录,关键,若不设置他会默认从网络中下载,国外网络可能下载不稳定导致失败 + // 我的 cef chrome 位于项目的chrome目录下 C:\Users\Administrator\Desktop\project\java\demo-desktop\chrome + // builder.setInstallDir(new File(System.getProperty("user.dir") + File.separator + "chrome")); + // 由于是手动设置cef的chrome,我们要跳过ins检查,防止版本不一致导致从镜像站下载 + // builder.setSkipInstallation(true); + + // 全局的 CefApp 每个程序只能有一个,线程安全 + CefApp build = builder.build(); + // 显示一些版本信息 + CefBuildInfo buildInfo = CefBuildInfo.fromClasspath(); + System.out.println(buildInfo); + CefApp.CefVersion cefVersion = build.getVersion(); + System.out.println(cefVersion); + + // 创建一个浏览器客户端实例 + cefClient = build.createClient(); + + // 创建一个浏览器实例,设置访问 url https://www.baidu.com + cefBrowser = cefClient.createBrowser("about:blank", useOSR, true); + } + + /** + * 加载地址 + * + * @param url 地址 + * @throws InterruptedException + */ + public void loadUrl(String url) throws InterruptedException { + // 必须延迟,否则可能因为切换太快无法加载成功 + Thread.sleep(300); + cefBrowser.loadURL(url); + } + + /** + * 刷新 + */ + public void reload() { + cefBrowser.reload(); + } + + /** + * 强制刷新 + */ + public void reloadForce() { + cefBrowser.reloadIgnoreCache(); + } + + public void addFocusListener(FocusListener listener) { + cefBrowser.getUIComponent().addFocusListener(listener); + } + + public void addMouseListener(MouseListener listener) { + cefBrowser.getUIComponent().addMouseListener(listener); + } + + public void addKeyListener(KeyListener listener) { + cefBrowser.getUIComponent().addKeyListener(listener); + } + + public void executeJavaScript(String javascript) { + cefBrowser.executeJavaScript(javascript, null, 1); + } + + public void close() { + // 关闭应用时要释放资源 + CefApp.getInstance().dispose(); + dispose(); + System.exit(0); + } +}