Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)
JxBrowser supports two rendering modes: lightweight and heavyweight.(JxBrowser支持两种渲染模式:轻量级和重量级。)
By default, heavyweight rendering mode is enabled, so when you create Browser instance using the following code, it creates a new Browser instance configured to use heavyweight rendering mode: (默认情况下,启用了重量级渲染模式,因此,使用以下代码创建浏览器实例时,它将创建一个配置为使用重量级渲染模式的新浏览器实例:)
Browser browser = new Browser();
To create a new Browser instance with specified rendering mode use the following way: (要使用指定的呈现方式创建新的Browser实例,请使用以下方式:)
// Creates Browser instance with enabled lightweight rendering mode Browser browser = new Browser(BrowserType.LIGHTWEIGHT); // Creates Browser instance with enabled heavyweight rendering mode Browser browser = new Browser(BrowserType.HEAVYWEIGHT);
The differences between lightweight and heavyweight rendering modes and recommendations about what rendering mode you should use in your application you can find below:(轻量级渲染模式和重量级渲染模式之间的差异以及有关在应用程序中应使用哪种渲染模式的建议,您可以在下面找到:)
Lightweight (off-screen)(轻巧(屏幕外))
In lightweight rendering mode Chromium engine renders web page off-screen using CPU. Images that represent different parts of loaded web page are stored in shared memory. JxBrowser reads the images from the shared memory and displays them using standard Java Swing/JavaFX 2D Graphics API.(在轻量级渲染模式下,Chromium引擎使用CPU在屏幕外渲染网页。代表加载的网页的不同部分的图像存储在共享内存中。 JxBrowser从共享内存中读取图像,并使用标准的Java Swing / JavaFX 2D图形API显示它们。)
To interact with displayed web page we listen to mouse and keyboard events using Java Events API, convert them to appropriate Chromium mouse/keyboard events, and send them to Chromium engine. With this approach we receive a pure lightweight solution.(为了与显示的网页进行交互,我们使用Java Events API监听鼠标和键盘事件,将它们转换为适当的Chromium鼠标/键盘事件,然后将它们发送给Chromium引擎。通过这种方法,我们得到了一个纯粹的轻量级解决方案。)
This rendering mode works great if you need a true lightweight Swing/JavaFX component that allows displaying modern HTML5/JavaScript/CSS web pages in your Java application. You can make this lightweight component semitransparent, display other components on top of it, put it into JLayeredPane or JInternalFrame, get notifications about mouse and keyboard events, etc. (如果您需要一个真正的轻量级Swing / JavaFX组件,该组件可以在Java应用程序中显示现代HTML5 / JavaScript / CSS网页,则此呈现模式非常有用。您可以使这个轻量级组件成为半透明的,在其之上显示其他组件,将其放入JLayeredPane或JInternalFrame中,获取有关鼠标和键盘事件的通知等。)
Please note, that JxBrowser does not support accessibility in a lightweight mode.(请注意,JxBrowser在轻量模式下不支持辅助功能。)
Headless environment(无头环境)
In case you need to use JxBrowser in a headless Linux environment, we recommend that you use the Lightweight rendering mode.(如果您需要在无头Linux环境中使用JxBrowser,我们建议您使用轻量级渲染模式。)
There are two main reasons for that:(这样做的主要原因有两个:)
- The Heavyweight (GPU-accelerated) rendering mode running in a headless environment via xvfb is useless because GPU acceleration in xvfb is disabled.(通过xvfb在无头环境中运行的重量级(GPU加速)渲染模式是无用的,因为禁用了xvfb中的GPU加速。)
- Very often in such environment the JxBrowser functionality that allows getting screenshots of the loaded web-pages is used. That functionality is available in the Lightweight rendering mode only.(在这种环境中,通常会使用JxBrowser功能来获取已加载网页的屏幕截图。该功能仅在轻量级渲染模式下可用。)
Limitations(局限性)
- Heavy animation on loaded web page cause high CPU and memory usage. It happens because Chromium engine needs to render a lot of images and save them into allocated shared memory.(加载的网页上的动画过多会导致较高的CPU和内存使用率。发生这种情况是因为Chromium引擎需要渲染很多图像并将其保存到分配的共享内存中。)
- In lightweight mode only Chromium plugins that supports windowless mode can be displayed. Since rendering is done off-screen, Flash player or other plugin must support windowless (off-screen) rendering as well. If plugin doesn't support off-screen rendering, it will not be displayed in lightweight mode.(在轻量模式下,只能显示支持无窗口模式的Chromium插件。由于渲染是在屏幕外完成的,因此Flash Player或其他插件也必须支持无窗口(屏幕外)渲染。如果插件不支持屏幕外渲染,则不会以轻量模式显示。)
- Mouse/keyboard/touch events are processes on Java side and forwarded to Chromium engine. Right now Java doesn't provide fully functional touch events support, so in lightweight rendering mode JxBrowser doesn't support some touch gestures. Same limitation is true for Drag&Drop functionality. In lightweight rendering mode, drag and drop is processed using Java API, so it doesn't work exactly as in Google Chrome. Drag&Drop supports only predefined set of flavours.(鼠标/键盘/触摸事件是Java端的进程,并转发给Chromium引擎。目前,Java不提供全功能的触摸事件支持,因此在轻量级渲染模式下,JxBrowser不支持某些触摸手势。拖放功能也有相同的限制。在轻量级渲染模式下,拖放操作是使用Java API处理的,因此它无法完全像在Google Chrome中一样工作。拖放仅支持预定义的一组口味。)
Heavyweight (GPU-accelerated)(重量级(GPU加速))
In the heavyweight rendering mode, which is default mode in JxBrowser, we embed a native window into your Java application window and configure Chromium engine to render content into this native window using GPU.(在重量级渲染模式(这是JxBrowser的默认模式)中,我们将本机窗口嵌入到Java应用程序窗口中,并配置Chromium引擎以使用GPU将内容渲染到该本机窗口中。)
User interaction with displayed web page is handled by the Chromium engine. The native Chromium window receives and processes all mouse/keyboard/touch events using platform specific native functionality when it is focused. Such behavior brings a better support of these events compared to the lightweight rendering mode where JxBrowser has to process Java events and forward them to Chromium engine.
However, when the native window is focused, the Java application does not receive the mouse/keyboard/touch events automatically, so these events are forwarded to the Java side after processing them in Chromium. (用户与显示的网页的交互由Chromium引擎处理。 Chromium原生窗口在聚焦时会使用平台特定的原生功能来接收和处理所有鼠标/键盘/触摸事件。与JxBrowser必须处理Java事件并将其转发到Chromium引擎的轻量级渲染模式相比,这种行为为这些事件提供了更好的支持。但是,当本机窗口成为焦点时,Java应用程序不会自动接收鼠标/键盘/触摸事件,因此在Chromium中处理这些事件后,这些事件将转发到Java端。)
Heavyweight (GPU-accelerated) rendering mode works much faster compared to lightweight rendering mode. Rendering performance is the same as in Google Chrome. You can display full screen video or HTML5 animation with 60fps. Compared to lightweight rendering mode, CPU and memory usage is much smaller, because web page's content is rendered via GPU.(与轻量级渲染模式相比,重量级(GPU加速)渲染模式工作更快。渲染性能与Google Chrome浏览器相同。您可以显示全屏视频或60fps的HTML5动画。与轻量级渲染模式相比,CPU和内存使用量要小得多,因为网页内容是通过GPU渲染的。)
So, if rendering performance is very important for your application, then we recommend that you use the default heavyweight rendering mode.(因此,如果渲染性能对于您的应用程序非常重要,那么我们建议您使用默认的重量级渲染模式。)
Limitations(局限性)
Lightweight and heavyweight mixing(轻量级和重量级混合)
Java Swing/JavaFX toolkits allows building UI based on lightweight components. When we embed a native window into Java frame it can lead to a well-known issue with mixing heavyweight and lightweight components. It's not recommended that you put heavyweight BrowserView component into JLayeredPane or JInternalFrame, or mix it with the lightweight components (e.g. trying to display a lightweight component over heavyweight BrowserView).(Java Swing / JavaFX工具箱允许基于轻量级组件构建UI。当我们将本机窗口嵌入Java框架时,可能会导致混合重量级组件和轻量级组件的问题。不建议您将重量级的BrowserView组件放入JLayeredPane或JInternalFrame中,或将其与轻量级组件混合(例如,尝试在重量级BrowserView上显示轻量级组件)。)
Layered windows in JavaFX applications(JavaFX应用程序中的分层窗口)
Configuring the stage with the StageStyle.TRANSPARENT style adds the WS_EX_LAYERED window style flag to JavaFX window. This flag is used to create a layered window. The layered window represents an off-screen window that draws its content off-screen. If we embed another window (it happens when heavyweight rendering mode is used in JxBrowser) into a layered window, its content will not be painted because of window types conflict.(使用StageStyle.TRANSPARENT样式配置舞台会将WS_EX_LAYERED窗口样式标志添加到JavaFX窗口。此标志用于创建分层窗口。分层窗口表示一个屏幕外窗口,该窗口将其内容绘制到屏幕外。如果我们将另一个窗口(在JxBrowser中使用重量级渲染模式时发生)嵌入到分层窗口中,则由于窗口类型冲突,将不会绘制其内容。)
Browser disposal in JavaFX(JavaFX中的浏览器处理)
Disposing Browser instance in the incorrect thread in JavaFX may lead to a deadlock on the native side. The Browser instances must be disposed on different threads depending on the operating system. In Linux and macOS the Browser.dispose() method must be called on the UI thread, while on Windows Browser must be disposed on the non-UI thread. For example:(将浏览器实例放置在JavaFX中的错误线程中可能会导致本机端出现死锁。根据操作系统的不同,必须将浏览器实例放置在不同的线程上。在Linux和macOS中,必须在UI线程上调用Browser.dispose()方法,而在Windows上必须将浏览器放在非UI线程上。例如:)
Platform.runLater(new Runnable() { @Override public void run() { browser.dispose(); } }); |
Linux and MacOS |
new Thread(new Runnable() { @Override public void run() { browser.dispose(); } }).start(); |
Windows |