如何在 Java Swing 的 JPanel 内呈现基本的 HTML 标记?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6360744/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:33:52  来源:igfitidea点击:

How to render basic HTML markup inside a JPanel in Java Swing?

htmlswingjpaneljava

提问by pathikrit

I want to do something really simple like this:

我想做一些非常简单的事情:

JPanel htmlPanel = new HtmlPanel("<html><body><h1>hello world</h1></body></html>");

I think I had seen code somewhere that did exactly this. What about CSS and JS? Can the JPanel be made to support this easily? Are there libraries somewhere that do what I am asking for?

我想我曾在某处看到过这样做的代码。CSS 和 JS 呢?可以让 JPanel 轻松支持这一点吗?是否有图书馆可以满足我的要求?

采纳答案by Costis Aivalis

Check hereto see how to add CSS support to your JEditorPane. You will need an HTMLEditorKit.

检查这里来看看如何对CSS的支持添加到您的JEdi​​torPane。您将需要一个HTMLEditorKit

I do not see why you would need JavaScript support for a JEditorPane. The basic use of it is to render Help pages written in HTML and CSS.

我不明白您为什么需要 JEditorPane 的 JavaScript 支持。它的基本用途是呈现用 HTML 和 CSS 编写的帮助页面。

If you need to have the real McCoy embedded into your application, try the Lobo Java Web Browser. Lobo does JavaScript and CSS and is open source 100% Java.

如果您需要将真正的 McCoy 嵌入到您的应用程序中,请尝试使用Lobo Java Web Browser。Lobo 执行 JavaScript 和 CSS,并且是 100% Java 开源。

November 2015: The latest fork of the Lobo Browser is Lobo Evolution.

2015 年 11 月:Lobo 浏览器的最新分支是Lobo Evolution

回答by Kaj

Sounds like you were thinking of the JEditorPane. It can display html from e.g. an URL. Swing tutorial, Editor panes.

听起来你在想JEditorPane. 它可以显示来自例如 URL 的 html。 Swing 教程,编辑器窗格。

The support for javascript is nonexistent.

对 javascript 的支持是不存在的。

You can try out third party components such as flying-saucer, which is a XML/XHTML and CSS 2.1 renderer in pure Java.

您可以试用第三方组件,例如fly-saucer,它是纯 Java 中的 XML/XHTML 和 CSS 2.1 渲染器。

回答by Axel

In case anyone is still looking for an easy solution to just enable display of HTML-Markup: Use a JLabel and enclose the text in '<html>' and '</html>' (body tag isn't needed).

如果有人仍在寻找一种简单的解决方案来启用 HTML 标记的显示:使用 JLabel 并将文本括在“<html>”和“</html>”中(不需要正文标记)。

Have a look at How to Use HTML in Swing Components. I just came across this when looking for an easy way to display data in tables, and it works like a charm.

查看如何在 Swing 组件中使用 HTML。我刚刚在寻找一种在表格中显示数据的简单方法时遇到了这个问题,它就像一个魅力。

回答by jefflunt

There was JWebPane, a Java component based on WebKit, but that project seems to have died a long time ago and/or has just gone quiet. Some people are still hoping it will be released, The other one I know of is WebKit for SWT, but it appears to be for Win32 only. :(

JWebPane,一个基于 WebKit 的 Java 组件,但该项目似乎很久以前就已经死了和/或刚刚消失。有些人仍然希望它会发布,我所知道的另一个是用于 SWT 的 WebKit,但它似乎仅适用于 Win32。:(