用于可滚动窗格的纯 Java HTML 查看器/渲染器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2438201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pure Java HTML viewer/renderer for use in a Scrollable pane
提问by dma_k
What pure Java HTML viewers and renderers are available? The requirements are:
有哪些纯 Java HTML 查看器和呈现器可用?要求是:
- It should implement the
JComponent
interface to be placed intoScrollable
pane. - It should be preferably a free solution; open source is a plus.
- Its availability as Maven artifact is a plus.
- 它应该实现
JComponent
要放置到Scrollable
窗格中的接口。 - 最好是免费的解决方案;开源是一个加分项。
- 它作为 Maven 工件的可用性是一个加分项。
I know only of a few components and projects, some of which are now defunct:
我只知道一些组件和项目,其中一些现已不存在:
- Built-in
JEditorPane
, supports HTML 3.2 (as of Java 1.4) - DJ Project(it is pure Java?)
- Ekit by hexidec(is based on
javax.swing.text.html.HTMLEditorKit
) - JSyndrome HTML Editor by Sferyx
JWebPane
(was it ever released)?- JDIC (abandoned; from some info hereI see that it is native)
- (PDF renderer) WebRenderer(former XHTMLRenderer)
- 内置
JEditorPane
,支持 HTML 3.2(从 Java 1.4 开始) - DJ 项目(它是纯 Java 的?)
- Hexidec 的 Ekit(基于
javax.swing.text.html.HTMLEditorKit
) - Sferyx 的 JSyndrome HTML 编辑器
JWebPane
(它曾经发布过吗)?- JDIC(废弃;从这里的一些信息我看到它是原生的)
- (PDF 渲染器) WebRenderer(前XHTMLRenderer)
回答by Sean A.O. Harney
Check out this article: http://devdaily.com/blog/post/jfc-swing/how-create-simple-swing-html-viewer-browser-java
看看这篇文章:http: //devdaily.com/blog/post/jfc-swing/how-create-simple-swing-html-viewer-browser-java
It uses JEditorPane and some other Swing classes to parse and render not only HTML, but also CSS.
它不仅使用 JEditorPane 和一些其他 Swing 类来解析和呈现 HTML,还使用 CSS。
回答by PSpeed
You can also access the native browser through something like: http://djproject.sourceforge.net/ns/
您还可以通过以下方式访问本机浏览器:http: //djproject.sourceforge.net/ns/
For certain web pages, this is sometimes the only way to go. There are always trade offs.
对于某些网页,有时这是唯一的方法。总是有取舍。
I have yet to find a browser component that renders well, is open source, and sufficiently flexible at the same time. Cobra comes close but there are pages that it won't render and it's tough (impossible?) to do things like get rid of its own scroll bars, etc..
我还没有找到一个渲染良好、开源且同时足够灵活的浏览器组件。Cobra 接近了,但有些页面它不会渲染,而且很难(不可能?)做一些事情,比如摆脱它自己的滚动条等。
回答by stano
CSSBox might be what you're looking for: http://cssbox.sourceforge.net
CSSBox 可能就是你要找的:http://cssbox.sourceforge.net
回答by Sheepy
Since Java 8, you can use JavaFX's WebView Component, which can also be used in Swing.
从 Java 8 开始,您可以使用 JavaFX 的WebView Component,它也可以在 Swing 中使用。
Code is as simple as:
代码很简单:
JFXPanel jfxPanel = new JFXPanel(); // Scrollable JCompenent
Platform.runLater( () -> { // FX components need to be managed by JavaFX
WebView webView = new WebView();
webView.getEngine().loadContent( "<html> Hello World!" );
webView.getEngine().load( "http://www.stackoverflow.com/" );
jfxPanel.setScene( new Scene( webView ) );
});
It is backed by the WebKit engine (version depends on JRE and is reasonably up to date). But keep in mind that it is not a full browser, so don't count on support of, say, HTML5 audio/video. Otherwise, it runs HTML + CSS + JS as good as your browser.
它由 WebKit 引擎支持(版本取决于 JRE 并且是最新的)。但请记住,它不是一个完整的浏览器,所以不要指望支持 HTML5 音频/视频。否则,它会像您的浏览器一样运行 HTML + CSS + JS。
Technically, the underlying engine is C++, not native Java. But it is bundled in Oracle's official JRE, requires no library, has zero config, is as cross-platform as Java FX, and is actively updated and maintained.
从技术上讲,底层引擎是 C++,而不是原生 Java。但它捆绑在 Oracle 的官方 JRE 中,不需要库,零配置,与 Java FX 一样跨平台,并积极更新和维护。
As good as native Java for most use cases, I think?
我认为在大多数用例中与原生 Java 一样好?
The information below is outdated, seeing that we now have WebView in Java.
下面的信息已经过时了,因为我们现在有了 Java 中的 WebView。
Tried Cobra/Lobo, CSSBox, and Flying Saucer, all pure Java. Others are either native or commercial.
尝试过Cobra/Lobo、CSSBox和Flying Saucer,都是纯 Java 。其他的要么是本地的,要么是商业的。
Content: Simple HTML generated on the fly (as string), embedded CSS 2.1, no JS.
内容:动态生成的简单 HTML(作为字符串),嵌入式 CSS 2.1,无 JS。
Short story: Flying Saucer is simplest to use and render is most correct, but you better have full control over content. Otherwise look for a native solution.
小故事:Flying Saucer 使用最简单,渲染最正确,但您最好对内容拥有完全控制权。否则寻找本地解决方案。
Long story:
很长的故事:
CSSBox seems to be more active, however it seems to depends on some 3rd party libraries. For example the demo depends on nekohtml which use apache xerces which changed the way the default Java 1.7 sax parser works and broke my program, but when I force it to use java's built in xerces I get ClassCastException (InlineBox to BlockBox)
. Can't get it to work at the end. Plus still haven't found a way to replace the document in an existing BrowserCanvas
.
CSSBox 似乎更活跃,但它似乎依赖于一些 3rd 方库。例如,演示依赖于使用 apache xerces 的 nekohtml,它改变了默认 Java 1.7 sax 解析器的工作方式并破坏了我的程序,但是当我强制它使用 java 的内置 xerces 时,我得到ClassCastException (InlineBox to BlockBox)
. 最后无法让它工作。另外还没有找到一种方法来替换现有 BrowserCanvas
.
Cobra is no longer maintained, have to manually fix an incompatibility issueto make it works in 1.7. Also need to grab mozilla Rhino (not using any JS) but that is all. After that it is fairly smooth, just need to ask Logger to hide paint messages. Render is correct and speed is fair - as long as the document is simple. When you start to use less common tags or more complicated layout, Cobra falls apart pretty quickly.
Cobra 不再维护,必须手动修复不兼容问题才能使其在 1.7 中工作。还需要获取 mozilla Rhino(不使用任何 JS),仅此而已。之后就相当流畅了,只需要让Logger隐藏绘制消息即可。渲染是正确的,速度是公平的——只要文档简单。当您开始使用不太常见的标签或更复杂的布局时,Cobra 很快就会崩溃。
Flying Saucer has the best CSS support of the three as of writing (Feb 2011). Setup is very easy (e.g. no need to setup document like cobo or domparser like cssbox) has few dependency - which also means no javascript. But Flying Saucer is very strict about what you feed it. The source must be a well-formed XML, for example style and script may have to be wrapped in CDATA and if you use html entities you must declare DTD (so no html5 doctype). However if you are embedding content that you can control then it may be your best choice.
在撰写本文时(2011 年 2 月),Flying Saucer 拥有三者中最好的 CSS 支持。设置非常简单(例如不需要像 cobo 那样设置文档或像 cssbox 这样的 domparser)几乎没有依赖性 - 这也意味着没有 javascript。但是飞碟对你喂它的东西非常严格。源必须是格式良好的 XML,例如样式和脚本可能必须包含在 CDATA 中,如果您使用 html 实体,则必须声明 DTD(因此没有 html5 doctype)。但是,如果您要嵌入可以控制的内容,那么它可能是您的最佳选择。
回答by haferblues
If you are using Swing, you can embed a JavaFX WebView.
如果您使用的是 Swing,则可以嵌入 JavaFX WebView。
1)Should implement JComponent interface to be placed into Scrollable pane.
1) 应实现 JComponent 接口以放置到 Scrollable 窗格中。
In order to add the WebView to Swing you need to add it to JFXPanel, which is a JComponent. To make the WebView fill the full JFXPanel, I used an AnchorPane like so:
为了将 WebView 添加到 Swing,您需要将它添加到 JFXPanel,它是一个 JComponent。为了让 WebView 填充完整的 JFXPanel,我使用了一个 AnchorPane,如下所示:
final AnchorPane anchorPane = new AnchorPane();
WebView webBrowser = new WebView();
//Set Layout Constraint
AnchorPane.setTopAnchor(webBrowser, 0.0);
AnchorPane.setBottomAnchor(webBrowser, 0.0);
AnchorPane.setLeftAnchor(webBrowser, 0.0);
AnchorPane.setRightAnchor(webBrowser, 0.0);
//Add WebView to AnchorPane
anchorPane.getChildren().add(webBrowser);
//Create Scene
final Scene scene = new Scene(anchorPane);
// Obtain the webEngine to navigate
final WebEngine webEngine = webBrowser.getEngine();
webEngine.load("http://www.google.com");
_jfxPanel.setScene(scene);
Whenever you run JavaFX code, make sure to run it in Platform.runLater().
无论何时运行 JavaFX 代码,请确保在 Platform.runLater() 中运行它。
2) Should be preferably a free solution; opensource is a plus.
2) 最好是一个免费的解决方案;开源是一个加分项。
Well, it's pure Oracle java.
嗯,它是纯 Oracle Java。
3) Availability as maven artifact is a plus.
3) 作为 Maven 工件的可用性是一个加分项。
See the StackOverflow answer Maven project with JavaFX (with jar file in `lib`)for advice on integrating JavaFX and Maven.
有关集成 JavaFX 和 Maven 的建议,请参阅 StackOverflow answer Maven project with JavaFX(在 `lib` 中有 jar 文件)。
From Java8 on JavaFX will be fully integrated in Java.
从 Java8 开始,JavaFX 将完全集成到 Java 中。
Additonal Pros: -supports HTML5 and JavaScript (uses webkit) -supports platform interoperability -even supports interacting with the DOM, run JavaScript, get notified of events from the Webview.
附加优点: - 支持 HTML5 和 JavaScript(使用 webkit) - 支持平台互操作性 - 甚至支持与 DOM 交互,运行 JavaScript,从 Webview 获取事件通知。
Cons: -JavaFX needs to be installed. But it comes bundled with java since v7u6 (August 2012).
缺点: -JavaFX 需要安装。但它自 v7u6(2012 年 8 月)以来与 java 捆绑在一起。
Other experiences:
其他体验:
I tried djproject, but had lots of problems with platform interoperability. Worked quite well on Windows, but only with major effort on Linux and I couldn't get it to work on Mac. For every platform you also need to build a 32bit and 64bit version of your jar. With lot of effort and a huge jar file you could possibly merge everything together in one jar. But this was far from being convenient.
我尝试过djproject,但在平台互操作性方面存在很多问题。在 Windows 上工作得很好,但只有在 Linux 上付出了很大的努力,我无法让它在 Mac 上工作。对于每个平台,您还需要构建 32 位和 64 位版本的 jar。通过大量的努力和一个巨大的 jar 文件,您可以将所有内容合并到一个 jar 中。但这远不方便。
Compared to the JavaFX solution I mentioned above, the DJProject was a way bigger pain.
与我上面提到的 JavaFX 解决方案相比,DJProject 的痛苦更大。
回答by Bruno Eberhard
Wow haferblues, I never thought I would find something I like about JavaFX. But the browser implementation is really nice. For those (like me) that never have used JavaFx before here the complete class (for the snippet of haferblues):
哇 haferblues,我从没想过我会在 JavaFX 上找到我喜欢的东西。但是浏览器的实现真的很好。对于那些在此之前从未使用过 JavaFx 的人(像我一样)完整的类(对于 haferblues 的片段):
import com.sun.javafx.application.PlatformImpl;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
public class SwingBrowser extends JFXPanel {
private static final long serialVersionUID = 1L;
public SwingBrowser(String url) {
PlatformImpl.startup(new Runnable() {
@Override
public void run() {
final AnchorPane anchorPane = new AnchorPane();
WebView webBrowser = new WebView();
// Set Layout Constraint
AnchorPane.setTopAnchor(webBrowser, 0.0);
AnchorPane.setBottomAnchor(webBrowser, 0.0);
AnchorPane.setLeftAnchor(webBrowser, 0.0);
AnchorPane.setRightAnchor(webBrowser, 0.0);
// Add WebView to AnchorPane
anchorPane.getChildren().add(webBrowser);
// Create Scene
final Scene scene = new Scene(anchorPane);
// Obtain the webEngine to navigate
final WebEngine webEngine = webBrowser.getEngine();
webEngine.load(url);
setScene(scene);
}
});
}
}
回答by Michael Fayad
The Flying Saucer was doing the job OK, but the following rendered text example was a huge turnoff for my mobile app development on Linux Java :
Flying Saucer 的工作还不错,但以下呈现的文本示例对于我在 Linux Java 上的移动应用程序开发来说是一个巨大的障碍:
Sometimes the period at the end changes line without the text beside
.
Sometimes the period at the end changes line without the text beside
.
Also, the text isn't selectable unlike for the JTextPanel.
此外,与 JTextPanel 不同,文本不可选择。
Parser only seems to accept UTF-8 encoding. I couldn't manage to force my own encoding when parsing.
解析器似乎只接受 UTF-8 编码。解析时我无法强制使用自己的编码。