文档
开始使用 JxBrowser
本指南展示了如何在 Gradle 或 Maven 项目中快速开始使用 JxBrowser。
前提条件
请确保您的系统满足软件和硬件要求。
选择您的项目构建工具
Gradle
Maven
请将 JxBrowser 添加到您的 build.gradle
中,如下所示:
repositories { maven { url = 'https://europe-maven.pkg.dev/jxbrowser/releases' } } dependencies { implementation "com.teamdev.jxbrowser:jxbrowser-cross-platform:7.32" }
请将 JxBrowser 添加到您的 pom.xml
中,如下所示:
<repositories> <repository> <id>com.teamdev</id> <url>https://europe-maven.pkg.dev/jxbrowser/releases</url> </repository> </repositories> <dependencies> <dependency> <groupId>com.teamdev.jxbrowser</groupId> <artifactId>jxbrowser-cross-platform</artifactId> <version>7.32</version> <type>pom</type> </dependency> </dependencies>
选择您正在或将要使用的软件
我想在不显示任何 GUI 的情况下使用 DOM 和 JS
我想在我的 Java UI 应用程序中显示网页
下面的简单示例演示了如何加载网页、待它完全加载之后,打印它的 HTML:
// Initialize Chromium. EngineOptions options = EngineOptions.newBuilder(HARDWARE_ACCELERATED) .licenseKey("your license key") .build(); Engine engine = Engine.newInstance(options); // Create a Browser instance. Browser browser = engine.newBrowser(); // Load a web page and wait until it is loaded completely. browser.navigation().loadUrlAndWait("https://html5test.com/"); // Print HTML of the loaded web page. browser.mainFrame().ifPresent(frame -> System.out.println(frame.html())); // Shutdown Chromium and release allocated resources. engine.close();
// Initialize Chromium. val options = EngineOptions.newBuilder(HARDWARE_ACCELERATED) .licenseKey("your license key") .build() val engine = Engine.newInstance(options) // Create a Browser instance. val browser = engine.newBrowser() // Load a web page and wait until it is loaded completely. browser.navigation().loadUrlAndWait("https://html5test.com/") // Print HTML of the loaded web page. browser.mainFrame().ifPresent { frame -> println(frame.html()) } // Shutdown Chromium and release allocated resources. engine.close()
在 GitHub 上查看完整示例。
选择您的 Java UI 工具包
Swing
JavaFX
SWT
请将 JxBrowser Swing 库添加到您的 build.gradle
中:
dependencies { implementation "com.teamdev.jxbrowser:jxbrowser-swing:7.32" }
请将 JxBrowser Swing 库添加到您的 pom.xml
中:
<dependency> <groupId>com.teamdev.jxbrowser</groupId> <artifactId>jxbrowser-swing</artifactId> <version>7.32</version> </dependency>
现在您可以将 JxBrowser Swing 组件嵌入到您的 Java Swing 应用程序中,如下所示:
// Initialize Chromium. EngineOptions options = EngineOptions.newBuilder(HARDWARE_ACCELERATED) .licenseKey("your license key") .build(); Engine engine = Engine.newInstance(options); // Create a Browser instance. Browser browser = engine.newBrowser(); SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("JxBrowser AWT/Swing"); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // Shutdown Chromium and release allocated resources. engine.close(); } }); // Create and embed Swing BrowserView component to display web content. frame.add(BrowserView.newInstance(browser)); frame.setSize(1280, 800); frame.setLocationRelativeTo(null); frame.setVisible(true); // Load the required web page. browser.navigation().loadUrl("https://html5test.com/"); });
// Initialize Chromium. val options = EngineOptions.newBuilder(RenderingMode.HARDWARE_ACCELERATED) .licenseKey("your license key") .build() val engine = Engine.newInstance(options) // Create a Browser instance. val browser = engine.newBrowser() SwingUtilities.invokeLater { val frame = JFrame("JxBrowser AWT/Swing") frame.addWindowListener(object: WindowAdapter() { override fun windowClosing(e: WindowEvent) { // Shutdown Chromium and release allocated resources. engine.close() } }) // Create and embed Swing BrowserView component to display web content. frame.add(BrowserView.newInstance(browser)) frame.setSize(1280, 800) frame.setLocationRelativeTo(null) frame.isVisible = true // Load the required web page. browser.navigation().loadUrl("https://html5test.com/") }
您应该会看到一下输出:

您可以随时从 GitHub 下载和使用已配置的带有 Swing GUI 的项目。
请将 JxBrowser JavaFX 库添加到您的 build.gradle
中:
dependencies { implementation "com.teamdev.jxbrowser:jxbrowser-javafx:7.32" }
请将 JxBrowser JavaFX 库添加到您的 pom.xml
中:
<dependency> <groupId>com.teamdev.jxbrowser</groupId> <artifactId>jxbrowser-javafx</artifactId> <version>7.32</version> </dependency>
现在您可以将 JxBrowser JavaFX 组件嵌入到您的 JavaFX 应用程序中,如下所示:
// Initialize Chromium. EngineOptions options = EngineOptions.newBuilder(HARDWARE_ACCELERATED) .licenseKey("your license key") .build(); Engine engine = Engine.newInstance(options); // Create a Browser instance. Browser browser = engine.newBrowser(); // Load the required web page. browser.navigation().loadUrl("https://html5test.com"); // Create and embed JavaFX BrowserView component to display web content. BrowserView view = BrowserView.newInstance(browser); Scene scene = new Scene(new BorderPane(view), 1280, 800); primaryStage.setTitle("JxBrowser JavaFX"); primaryStage.setScene(scene); primaryStage.show(); // Shutdown Chromium and release allocated resources. primaryStage.setOnCloseRequest(event -> engine.close());
// Initialize Chromium. val options = EngineOptions.newBuilder(HARDWARE_ACCELERATED) .licenseKey("your license key") .build() val engine = Engine.newInstance(options) // Create a Browser instance. val browser = engine.newBrowser() // Load the required web page. browser.navigation().loadUrl("https://html5test.com") // Create and embed JavaFX BrowserView component to display web content. val view = BrowserView.newInstance(browser) val scene = Scene(BorderPane(view), 1280.0, 800.0) primaryStage.setTitle("JxBrowser JavaFX") primaryStage.setScene(scene) primaryStage.show() // Shutdown Chromium and release allocated resources. primaryStage.setOnCloseRequest { engine.close() }
您应该会看到一下输出:

您可以随时从 GitHub 下载和使用已配置的带有 JavaFX GUI 的项目。
请将 JxBrowser SWT 库添加到您的 build.gradle
中:
dependencies { implementation "com.teamdev.jxbrowser:jxbrowser-swt:7.32" }
请将 JxBrowser SWT 库添加到您的 pom.xml
中:
<dependency> <groupId>com.teamdev.jxbrowser</groupId> <artifactId>jxbrowser-swt</artifactId> <version>7.32</version> </dependency>
现在您可以将 JxBrowser SWT 组件嵌入到您的 Java Swing 应用程序中,如下所示:
// Initialize Chromium. EngineOptions options = EngineOptions.newBuilder(HARDWARE_ACCELERATED) .licenseKey("your license key") .build(); Engine engine = Engine.newInstance(options); // Create a Browser instance. Browser browser = engine.newBrowser(); // Load the required web page. browser.navigation().loadUrl("https://html5test.com"); Display display = new Display(); Shell shell = new Shell(display); shell.setText("JxBrowser SWT"); shell.setLayout(new FillLayout()); // Create and embed SWT BrowserView widget to display web content. BrowserView view = BrowserView.newInstance(shell, browser); view.setSize(1280, 800); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } // Shutdown Chromium and release allocated resources. engine.close(); display.dispose();
// Initialize Chromium. val options = EngineOptions.newBuilder(HARDWARE_ACCELERATED) .licenseKey("your license key") .build() val engine = Engine.newInstance(options) // Create a Browser instance. val browser = engine.newBrowser() // Load the required web page. browser.navigation().loadUrl("https://html5test.com") val display = Display() val shell = Shell(display) shell.setText("JxBrowser SWT") shell.setLayout(FillLayout()) // Create and embed SWT BrowserView widget to display web content. val view = BrowserView.newInstance(shell, browser) view.setSize(1280, 800) shell.pack() shell.open() while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep() } } // Shutdown Chromium and release allocated resources. engine.close() display.dispose()
您应该会看到一下输出:

您可以随时从 GitHub 下载和使用已配置的带有 SWT GUI 的项目。