Java 在 Swing 应用程序中呈现 html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1790500/
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
Render html in Swing application
提问by Maksym Govorischev
I have a swing application that sends commands to server and receives result in XML format. I need to transform this into HTML via XSLT and then display result HTML on the panel. The problem is that the only Swing component which is able to display HTML - JEditorPane - takes either URL or javax.swing.text.StyledDocument as a source. Option with URL doesn't work for me because I have to save my html as a file on the file system first and I'd like to avoid this.
我有一个 Swing 应用程序,它向服务器发送命令并以 XML 格式接收结果。我需要通过 XSLT 将其转换为 HTML,然后在面板上显示结果 HTML。问题是唯一能够显示 HTML 的 Swing 组件 - JEditorPane - 将 URL 或 javax.swing.text.StyledDocument 作为源。带有 URL 的选项对我不起作用,因为我必须首先将我的 html 保存为文件系统上的文件,我想避免这种情况。
So I have a gap between in-memory result of XSL transformation and javax.swing.text.StyledDocument, which can be rendered by JEditorPane or JTextPane.
所以我在 XSL 转换的内存结果和 javax.swing.text.StyledDocument 之间有一个差距,它可以由 JEditorPane 或 JTextPane 呈现。
How to transform one to another? Or are there any other Swing solutions to display HTML from some in-memory source(DOM or String or whatever)?
如何将一种转化为另一种?或者是否有任何其他 Swing 解决方案来显示来自某些内存源(DOM 或 String 或其他)的 HTML?
Thank you in advance for help.
预先感谢您的帮助。
采纳答案by PSpeed
Is there a reason that JEditorPane.setText() does not work for you?
JEditorPane.setText() 对您不起作用有什么原因吗?
I use JEditorPane all the time and I've never pulled the displayed data from a file or URL. So it is possible. Just need to figure out why it's not working for you.
我一直使用 JEditorPane,但从未从文件或 URL 中提取显示的数据。所以这是可能的。只需要弄清楚为什么它不适合你。
To be specific:
再具体一点:
editor.setContentType( "text/html" );
editor.setText( "<html><body>Hello, world</body></html>" );
回答by EJB
What about JeditorPane.setText()
?
怎么样 JeditorPane.setText()
?