依赖
<dependency>
<groupId>org.bitbucket.johness</groupId>
<artifactId>java-cef</artifactId>
<!--<version>49.87.win32.2</version>-->
<version>49.87.win64.2</version>
</dependency>
这个版本最低支持xp系统,所以选择这个版本。最新版本可以在官网下载源码编译
窗口程序
package com.cares.fids.client.core;
import org.cef.browser.CefBrowser;
import javax.annotation.Resource;
import javax.swing.*;
import java.awt.*;
/**
* @author wangcj
*/
public class WebClient extends JFrame {
private final CefBrowser browser;
@Resource
private CefBrowser cefBrowser;
public WebClient(CefBrowser browser) throws HeadlessException {
this.browser = browser;
}
private static void initialSkin() {
try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
public void initWebClient() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
// 全屏设置
setLocationRelativeTo(null);
setContentPane(new JPanel());
setUndecorated(true);
setResizable(false);
gd.setFullScreenWindow(this);
setLayout(new BorderLayout());
getContentPane().add(cefBrowser.getUIComponent(), BorderLayout.CENTER);
setVisible(true);
}
public void start() {
initialSkin();
EventQueue.invokeLater(() -> {
initWebClient();
});
}
}
spring 配置
package com.cares.fids.client.core;
import com.cares.fids.client.handler.*;
import lombok.extern.slf4j.Slf4j;
import org.cef.CefApp;
import org.cef.CefClient;
import org.cef.CefSettings;
import org.cef.browser.CefBrowser;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
/**
* @author wangcj
* @desc
* @date 2020/12/23 14:49
**/
@Slf4j
@Configuration
public class WebClientContext {
public static final String cache = System.getProperties().getProperty("user.home") + File.separator + "AppData" + File.separator + "Local" + File.separator + "fids";
public static final String log_file = cache + File.separator + "fids_log.log";
@Bean
public CefSettings cefSettings() {
CefSettings settings = new CefSettings();
settings.windowless_rendering_enabled = false;
settings.command_line_args_disabled = true;
settings.cache_path = cache;
settings.log_file = log_file;
settings.log_severity = CefSettings.LogSeverity.LOGSEVERITY_DISABLE;
settings.locale = "zh-CN";
settings.product_version = "1.0";
settings.user_agent = "FIDS";
return settings;
}
@Bean(destroyMethod = "dispose")
public CefApp cefApp(CefSettings settings) {
String[] args = new String[0];
CefApp myApp = CefApp.getInstance(args, settings);
CefApp.CefVersion version = myApp.getVersion();
CefApp.addAppHandler(new AppHandlerAdapter(args));
log.info("Using:\n" + version);
return myApp;
}
@Bean
public CefClient cefClient(CefApp cefApp) {
CefClient client_ = cefApp.createClient();
client_.addContextMenuHandler(new ContextMenuHandlerAdapter());
client_.addDownloadHandler(new DownloadHandler());
client_.addJSDialogHandler(new JSDialogHandlerAdapter());
client_.addKeyboardHandler(new KeyboardHandler());
client_.addRequestHandler(new RequestHandlerAdapter());
client_.addLifeSpanHandler(new LifeSpanHandler(client_, false, false));
client_.addDisplayHandler(new DisplayHandlerAdapter());
client_.addLoadHandler(new LoadHandlerAdapter());
return client_;
}
@Bean
public CefBrowser cefBrowser(CefClient cefClient) {
String defaultUrl = "https://www.baidu.com";
CefBrowser browser_ = cefClient.createBrowser(defaultUrl, false, false, null);
return browser_;
}
@Bean
public WebClient webClient(CefBrowser cefBrowser) {
WebClient webClient = new WebClient(cefBrowser);
webClient.start();
return webClient;
}
}
高版本的jcef
高版本的jcef,maven库上没有现成的,需要自己编译
windows 平台使用的时候要一次加载三个库
System.load(“jawt.dll”);
System.load(“cef.dll”);
System.load(“jcef.dll”);
参考类 CefApp 的私有构造方法
private CefApp(String[] args, CefSettings settings) throws UnsatisfiedLinkError {
super(args);
if (settings != null) settings_ = settings.clone();
if (OS.isWindows()) {
SystemBootstrap.loadLibrary("jawt");
SystemBootstrap.loadLibrary("chrome_elf");
SystemBootstrap.loadLibrary("libcef");
// Other platforms load this library in CefApp.startup().
SystemBootstrap.loadLibrary("jcef");
} else if (OS.isLinux()) {
SystemBootstrap.loadLibrary("cef");
}
if (appHandler_ == null) {
appHandler_ = this;
}
// Execute on the AWT event dispatching thread.
try {
Runnable r = new Runnable() {
@Override
public void run() {
// Perform native pre-initialization.
if (!N_PreInitialize())
throw new IllegalStateException("Failed to pre-initialize native code");
}
};
if (SwingUtilities.isEventDispatchThread())
r.run();
else
SwingUtilities.invokeAndWait(r);
} catch (Exception e) {
e.printStackTrace();
}
}
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!