一、写在前面
以前写windows桌面应用首选用C#,当然是最好用的解决方案之一。这些年用java,才发现着实费劲,最近这些年随着硬件的能力提升,使用H5开发各种移动应用,桌面应用的方法也逐渐变得可行。本文记载了我用JCEF开发windows桌面应用的一种方法。
先了解一些:
CEF是什么? Chromium Embedded Framework (CEF)是个基于Google Chromium项目的开源Web browser控件,支持Windows, Linux, Mac平台。除了提供C/C++接口外,也有其他语言的移植版。因为基于Chromium,所以CEF支持 Webkit & Chrome中实现的 HTML5 的特性,并且在性能上面,也比较接近Chrome。
JCEF是什么?它是 CEF 在Java语言的实现。它基于 Chromium 嵌入式框架 (CEF),允许您将 Web 浏览器嵌入到 Java Swing 桌面应用程序中。
也就是说我的方式是:
1、写一个Java应用,集成JCEF 做界面,进行UI交互。前端用VUE.js实现。
2、在这个应用中集成 Springboot 做业务处理集成。
3、打出的jar 启动后,即启动一个内嵌的浏览器,展示界面,同时也是Java 程序。
二、使用
简单省事的方式是使用maven 来集成.
GITHUB: https://github.com/jcefmaven/jcefmaven
<dependency>
<groupId>me.friwi</groupId>
<artifactId>jcefmaven</artifactId>
<version>116.0.19</version>
</dependency>
代码示例:
package org.example;
import me.friwi.jcefmaven.CefAppBuilder;
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 org.cef.browser.CefFrame;
import org.cef.browser.CefMessageRouter;
import org.cef.handler.CefDisplayHandlerAdapter;
import org.cef.handler.CefFocusHandlerAdapter;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
public class MainFrame extends JFrame {
private static final long serialVersionUID = -5570653778104813836L;
private final JTextField address_;
private final CefApp cefApp_;
private final CefClient client_;
private final CefBrowser browser_;
private final Component browerUI_;
private boolean browserFocus_ = true;
MainFrame(String startURL, boolean useOSR, boolean isTransparent, String[] args) throws UnsupportedPlatformException, CefInitializationException, IOException, InterruptedException {
CefAppBuilder builder = new CefAppBuilder();
builder.getCefSettings().windowless_rendering_enabled = useOSR;
builder.setAppHandler(new MavenCefAppHandlerAdapter() {
@Override
public void stateHasChanged(org.cef.CefApp.CefAppState state) {
if (state == CefApp.CefAppState.TERMINATED) System.exit(0);
}
});
if (args.length > 0) {
builder.addJcefArgs(args);
}
cefApp_ = builder.build();
client_ = cefApp_.createClient();
CefMessageRouter msgRouter = CefMessageRouter.create();
client_.addMessageRouter(msgRouter);
browser_ = client_.createBrowser(startURL, useOSR, isTransparent);
browerUI_ = browser_.getUIComponent();
address_ = new JTextField(startURL, 100);
address_.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
browser_.loadURL(address_.getText());
}
});
client_.addDisplayHandler(new CefDisplayHandlerAdapter() {
@Override
public void onAddressChange(CefBrowser browser, CefFrame frame, String url) {
address_.setText(url);
}
});
address_.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
if (!browserFocus_) return;
browserFocus_ = false;
KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
address_.requestFocus();
}
});
client_.addFocusHandler(new CefFocusHandlerAdapter() {
@Override
public void onGotFocus(CefBrowser browser) {
if (browserFocus_) return;
browserFocus_ = true;
KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
browser.setFocus(true);
}
@Override
public void onTakeFocus(CefBrowser browser, boolean next) {
browserFocus_ = false;
}
});
getContentPane().add(address_, BorderLayout.NORTH);
getContentPane().add(browerUI_, BorderLayout.CENTER);
pack();
setTitle("我的工具");
setSize(1400, 800);
setLocationRelativeTo(null);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
CefApp.getInstance().dispose();
dispose();
}
});
}
public static void main(String[] args) throws UnsupportedPlatformException, CefInitializationException, IOException, InterruptedException {
boolean useOsr = false;
new MainFrame("http://www.baidu.com/", useOsr, false, args);
}
}
启动应用后,会展示一个浏览器窗口
image.png
三、参考:
https://blog.csdn.net/u013642500/article/details/102655124
https://zhuanlan.zhihu.com/p/555392208
网友评论