如何使用StyledDocument与HTML输出? [英] How to output using StyledDocument with HTML?
本文介绍了如何使用StyledDocument与HTML输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 JTextPane
,我想使用 StyledDocument
输出文本。这是我的 StyledDocument
对象
StyledDocument dox =(StyledDocument)textArea。 getDocument();
Style style = dox.addStyle(StyleName,null);
StyleConstants.setFontFamily(style,Font.SANS_SERIF);
StyleConstants.setFontSize(style,8);
dox.insertString(dox.getLength(),< b>一些文本< / b>,null);
现在的问题是如果我用html代码编辑文本,它不会显示我的方式想。我希望文本显示为粗体而不是字面上的某些文本。
有没有办法做到这一点?
解决方案
我最终使用HTMLEditorKit自己弄清楚了,这里是进一步参考的答案。
StyledDocument dox =(StyledDocument)textArea.getDocument();
textPane.setEditorKit(new HTMLEditorKit());
textPane.setText(< b>一些文字< / b>);
I have a JTextPane
, and I would like to output text on it using StyledDocument
. Here is my StyledDocument
object
StyledDocument dox = (StyledDocument) textArea.getDocument();
Style style = dox.addStyle("StyleName", null);
StyleConstants.setFontFamily(style, Font.SANS_SERIF);
StyleConstants.setFontSize(style, 8);
dox.insertString(dox.getLength(), "<b>Some Text</b>", null);
The problem right now is if I edit the text with html code, it does not display the way I want. I want the text to be displayed as bolded instead of literally "Some Text".
Is there a way to do this?
解决方案
I did figure it out on my own in the end using HTMLEditorKit, here's the answer for futher reference
StyledDocument dox = (StyledDocument) textArea.getDocument();
textPane.setEditorKit(new HTMLEditorKit());
textPane.setText("<b>Some Text</b>");
这篇关于如何使用StyledDocument与HTML输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文