SWT集成chromium

java 专栏收录该内容
15 篇文章 1 订阅

1 参考官方文档

https://www.eclipse.org/swt/faq.php#howusechromium

Q: How do I explicitly use Chromium as the Browser’s underlying renderer?
A: To specify that a Chromium renderer be used by a Browser instance, create it with style SWT.CHROMIUM (@since 4.17) or set the Java property org.eclipse.swt.browser.DefaultType=chromium.
You can get the SWT-Chromium libraries from the Eclipse SDK or from the standalone SWT Chromium support libraries section on the download page.

To use the Chromium libraries from the Eclipse SDK:

Install the CEF binaries in Eclipse from the p2 repo - CEF p2 repo from Make technology
Add the required jars to classpath of project:
SWT-Chromium fragment (org.eclipse.swt.browser.chromium....jar)
SWT fragment (org.eclipse.swt....jar)
CEF binary (com.make.chromium.cef....jar)
To use the Chromium libraries from the standalone SWT downloads:

Get CEF binaries for your platform from the p2 repo:
CEF GTK binaries
CEF Mac binaries
CEF Windows binaries
Add the required jars to classpath of project:
SWT-Chromium standalone jar (swt-chromium.jar)
SWT standalone jar (swt.jar)
CEF binary (com.make.chromium.cef....jar)
To launch Eclipse with Chromium as the default browser type:

Install the CEF binaries in Eclipse from the p2 repo - CEF p2 repo from Make technology
In eclipse.ini, add -Dorg.eclipse.swt.browser.DefaultType=chromium under -vmargs.

2 下载

下载swt, 下载 swt-chromium , 下载 CEF Windows binaries( 比较难).
https://download.eclipse.org/eclipse/downloads/drops4/S-4.17RC1-202008261800/

3 编写代码

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Main {

	public static void main(String[] args) {

		final Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("Snippet 148");
		shell.setLayout(new FillLayout());
		Browser browser = null;
		try {
			browser = new Browser(shell, SWT.CHROMIUM);
		} catch (SWTError e) {
			/*
			 * The Browser widget throws an SWTError if it fails to instantiate properly.
			 * Application code should catch this SWTError and disable any feature requiring
			 * the Browser widget. Platform requirements for the SWT Browser widget are
			 * available from the SWT FAQ website.
			 */
		}
		if (browser != null) {
			/* The Browser widget can be used */
			browser.setUrl("http://www.baidu.com");
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();

	}

}

4 测试工程

在这里插入图片描述

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

随风九天

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值