基本上,我有一个JTextPane来保存一些我希望设置样式的文本。 JTextArea对我来说会更好,但是我被告知您不能在其中设置文本样式?
但是,JTextPane的样式似乎不正确。例如,下面的代码只是返回包含了HTML的代码:
public static void main(String[] args) {
JFrame j = new JFrame("Hello!");
j.setSize(200,200);
JTextPane k = new JTextPane();
k.setText("<html><strong>Hey!</strong></html>");
j.add(k);
j.setVisible(true);
}
我希望能够在用户与界面交互时在JTextPane中设置某些文本的样式,但是到目前为止,它只是返回了仍包含HTML的字符串!救命!
最佳答案
如果要在JTextPane中显示Html
内容,则必须设置JTextPane#setContentType("text/html");,例如here
编辑:
for JEditorPanes / JTextPanes
是实现StyledDocument,MutableAttributeSet并使用自定义的Highlighte r的另一种方法,例如here
上午的方式是不使用Html syntax