前面的一个案例内容比较复杂,这次来个简单的直接使用的;

一、新建一个JPanel,里面放置JCEF浏览器,核心代码如下:

package com.test;
import java.awt.Component;
import javax.swing.JPanel;
import org.cef.CefApp;
import org.cef.CefClient;
import org.cef.CefSettings;
import org.cef.browser.CefBrowser;
import java.awt.BorderLayout;

/**
 * JCEF 浏览器 Panel
 * @author Admin
 */
public class JCEFChromiumPanel extends JPanel {
   private static final long serialVersionUID = -7986009724149446244L;
   private Component browserUI;
   private CefSettings settings;
   private CefApp cefApp;
   private CefClient client;
   private CefBrowser browser;

   public JCEFChromiumPanel() {
      initGUI();
      initBrowser();
   }
   private void initBrowser() {
      String url = "https://www.baidu.com/";
      boolean isOffscreenRendered = false;
      boolean isTransparent = false;
      settings = new CefSettings();
      settings.remote_debugging_port = 26688;
      settings.windowless_rendering_enabled = isOffscreenRendered;
      String[] disableWebSecurity = { "--disable-web-security", "--allow-file-access-from-files" };
      cefApp = CefApp.getInstance(settings);
      CefApp.getInstance(disableWebSecurity, settings);
      client = cefApp.createClient();
      browser = client.createBrowser(url, isOffscreenRendered, isTransparent);
      browserUI = browser.getUIComponent();
      add(browserUI, BorderLayout.CENTER);
   }
   private void initGUI() {
      setLayout(new BorderLayout(0, 0));
   }
   public void loadURL(String url) {
      browser.loadURL(url);
   }

}

二、新建一个JFrame,用来显示浏览器:

package com.test;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Dimension;
import javax.swing.border.MatteBorder;
import java.awt.Color;
import javax.swing.border.LineBorder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TestFrame extends JFrame {
   private static final long serialVersionUID = 797311945367465965L;
   private JPanel contentPane;
   private JCEFChromiumPanel jcefChromiumPanel;

   public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            try {
               TestFrame frame = new TestFrame();
               frame.setVisible(true);
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      });
   }

   public TestFrame() {
      initGUI();
   }
   private void initGUI() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setBounds(100, 100, 696, 488);
      contentPane = new JPanel();
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
      contentPane.setLayout(new BorderLayout(0, 0));
      contentPane.add(getJcefChromiumPanel(), BorderLayout.CENTER);
   }
   public JCEFChromiumPanel getJcefChromiumPanel() {
      if (jcefChromiumPanel == null) {
         jcefChromiumPanel = new JCEFChromiumPanel();
      }
      return jcefChromiumPanel;
   }
} 



注:如果想使用Java Selenium操控 JCEF内嵌浏览器,请参照:《10.Java Selenium 操作已经启动的 JCEF 内嵌浏览器》