如果自己没有编译后的JCEF文件,可去这里下载:https://pan.baidu.com/s/1C7NyoNWEc7sph7GvZ1oaqg 提取码:hfk2
本示例教程开源项目地址:
github:https://github.com/lieyanfeimao/JcefTest.git
码云:https://gitee.com/edadmin/JcefTest.git
首先,打开世界上最好的开发工具eclipse,新建普通java工程。工程编码建议设置为UTF-8
新建lib目录,引入所需jar包,Add to build path
项目>Properties(属性)>Java Build Path,展开Jdk,选中Native library location,点击Edit,选择JCEF的二进制文件目录
使用世界上最好的开发语言编写测试类,运行。
public class TestFrame extends JFrame{
/**
*
*/
private static final long serialVersionUID = -7410082787754606408L;
public static void main(String[] args) {
new TestFrame();
}
public TestFrame() {
//是否Linux系统
boolean useOSR=OS.isLinux();
//是否透明
boolean isTransparent=false;
//添加Handler,在CEFAPP状态为终止时退出程序
CefApp.addAppHandler(new CefAppHandlerAdapter(null) {
@Override
public void stateHasChanged(org.cef.CefApp.CefAppState state) {
// Shutdown the app if the native CEF part is terminated
if (state == CefAppState.TERMINATED) System.exit(0);
}
});
CefSettings settings = new CefSettings();
settings.windowless_rendering_enabled = useOSR;
//获取CefApp实例
CefApp cefApp=CefApp.getInstance(settings);
//创建客户端实例
CefClient cefClient = cefApp.createClient();
//创建浏览器实例
CefBrowser cefBrowser = cefClient.createBrowser("http://www.baidu.com", useOSR, isTransparent);
//将浏览器UI添加到窗口中
getContentPane().add(cefBrowser.getUIComponent(), BorderLayout.CENTER);
pack();
setTitle("测试JCEF打开百度");
setSize(800, 600);
setVisible(true);
//添加一个窗口关闭监听事件
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
CefApp.getInstance().dispose();
dispose();
}
});
}
}