用于可滚动窗格的纯 Java HTML 查看器/渲染器

发布于 2024-08-24 14:59:35 字数 1539 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

伴梦长久 2024-08-31 14:59:58

Flying Saucer 的工作做得很好,但以下渲染的文本示例对于我在 Linux Java 上的移动应用程序开发来说是一个巨大的障碍:

有时末尾的句点会更改行,旁边没有文本

此外,与 JTextPanel 不同的是,文本是不可选择的。

解析器似乎只接受 UTF-8 编码。我无法在解析时强制使用自己的编码。

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 :

Sometimes the period at the end changes line without the text beside
.

Also, the text isn't selectable unlike for the JTextPanel.

Parser only seems to accept UTF-8 encoding. I couldn't manage to force my own encoding when parsing.

草莓酥 2024-08-31 14:59:57

哇 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);
            }
        });
    }
}

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):

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);
            }
        });
    }
}
独享拥抱 2024-08-31 14:59:55

您还可以通过以下方式访问本机浏览器:
http://djproject.sourceforge.net/ns/

对于某些网页,有时这是唯一的出路。总有一些权衡。

我还没有找到一个渲染良好、开源且同时足够灵活的浏览器组件。 Cobra 很接近,但有些页面它不会渲染,而且很难(不可能?)做一些事情,比如摆脱它自己的滚动条等。

You can also access the native browser through something like:
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..

能怎样 2024-08-31 14:59:52

查看这篇文章: http ://devdaily.com/blog/post/jfc-swing/how-create-simple-swing-html-viewer-browser-java

它使用 JEditorPane 和其他一些 Swing 类来解析和渲染不仅 HTML,还有CSS。

Check out this article: 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.

世俗缘 2024-08-31 14:59:51

CSSBox 可能就是您要找的:http://cssbox.sourceforge.net

CSSBox might be what you're looking for: http://cssbox.sourceforge.net

长发绾君心 2024-08-31 14:59:48

如果您使用 Swing,则可以嵌入 JavaFX WebView。

1)应该实现JComponent接口以放置到可滚动窗格中。

为了将 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);

每当您运行 JavaFX 代码时,请确保在 Platform.runLater() 中运行它。

2)最好是免费的解决方案;开源是一个优点。

嗯,它是纯Oracle java。

3) 作为 Maven 工件的可用性是一个优点。

请参阅 StackOverflow 答案 使用 JavaFX 的 Maven 项目(jar 文件位于 ` lib`) 有关集成 JavaFX 和 Maven 的建议。

从Java8开始,JavaFX将完全集成在Java中。

其他优点:
- 支持 HTML5 和 JavaScript(使用 webkit)
-支持平台互操作性
-甚至支持与 DOM 交互、运行 JavaScript、从 Webview 获取事件通知。

缺点:
- 需要安装JavaFX。但自 v7u6(2012 年 8 月)以来,它与 java 捆绑在一起。

其他经验:

我尝试了 djproject,但在平台互操作性方面遇到了很多问题。在 Windows 上运行得很好,但在 Linux 上需要付出很大的努力,我无法让它在 Mac 上运行。对于每个平台,您还需要构建 32 位和 64 位版本的 jar。通过大量的努力和一个巨大的 jar 文件,您可以将所有内容合并到一个 jar 中。但这远不方便。

与我上面提到的 JavaFX 解决方案相比,DJProject 是一个更大的痛苦。

If you are using Swing, you can embed a JavaFX WebView.

1)Should implement JComponent interface to be placed into Scrollable pane.

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:

                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().

2) Should be preferably a free solution; opensource is a plus.

Well, it's pure Oracle java.

3) Availability as maven artifact is a plus.

See the StackOverflow answer Maven project with JavaFX (with jar file in `lib`) for advice on integrating JavaFX and Maven.

From Java8 on JavaFX will be fully integrated in 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.

Cons:
-JavaFX needs to be installed. But it comes bundled with java since v7u6 (August 2012).

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.

Compared to the JavaFX solution I mentioned above, the DJProject was a way bigger pain.

烂人 2024-08-31 14:59:45

从 Java 8 开始,您可以使用 JavaFX 的 WebView 组件,它可以也可在 Swing 中使用

代码非常简单:

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 ) );
});

它由 WebKit 引擎支持(版本取决于 JRE 并且是相当最新的)。
但请记住,它不是一个完整的浏览器,因此不要指望支持 HTML5 音频/视频等。
否则,它可以像您的浏览器一样运行 HTML + CSS + JS。

从技术上讲,底层引擎是 C++,而不是原生 Java。
但它捆绑在 Oracle 官方 JRE 中,不需要任何库,零配置,与 Java FX 一样跨平台,并且积极更新和维护。

我认为在大多数用例中与本机 Java 一样好?


下面的信息已经过时了,因为我们现在已经有了 Java 中的 WebView。

尝试过 Cobra/LoboCSSBoxFlying Saucer,都是纯 Java。其他的要么是本地的,要么是商业的。

内容:动态生成的简单 HTML(作为字符串),嵌入 CSS 2.1,无 JS。

简短的故事:飞碟使用起来最简单,渲染也最正确,但你最好能完全控制内容。否则寻找本机解决方案。

长话短说:

CSSBox 似乎更活跃,但它似乎依赖于一些第三方库。例如,演示依赖于 nekohtml,它使用 apache xerces,它改变了默认 Java 1.7 sax 解析器的工作方式并破坏了我的程序,但是当我强制它使用 java 内置的 xerces 时,我得到 ClassCastException (InlineBox to BlockBox)< /代码>。最后无法让它发挥作用。另外,仍然没有找到替换现有 BrowserCanvas 中文档的方法。

Cobra 不再维护,必须手动修复不兼容性问题才能使其在 1.7 中运行。还需要获取 mozilla Rhino(不使用任何 JS),但仅此而已。之后就比较顺利了,只需要让Logger隐藏paint消息即可。渲染是正确的并且速度是公平的——只要文档简单。当您开始使用不太常见的标签或更复杂的布局时,Cobra 很快就会崩溃。

截至撰写本文时(2011 年 2 月),Flying Saucer 拥有三者中最好的 CSS 支持。设置非常简单(例如,不需要设置像 cobo 或 domparser 这样的文档,比如 cssbox),几乎没有依赖性 - 这也意味着不需要 javascript。但飞碟对喂它的食物非常严格。源必须是格式良好的 XML,例如样式和脚本可能必须包装在 CDATA 中,如果您使用 html 实体,则必须声明 DTD(因此没有 html5 doctype)。但是,如果您要嵌入您可以控制的内容,那么它可能是您的最佳选择。

Since Java 8, you can use JavaFX's WebView Component, which can also be used in 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.

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.

As good as native Java for most use cases, I think?


The information below is outdated, seeing that we now have WebView in Java.

Tried Cobra/Lobo, CSSBox, and Flying Saucer, all pure Java. Others are either native or commercial.

Content: Simple HTML generated on the fly (as string), embedded CSS 2.1, no 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.

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.

Cobra is no longer maintained, have to manually fix an incompatibility issue to 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文