【问题标题】:JTextPane alignment and HTMLJTextPane 对齐和 HTML
【发布时间】:2013-12-11 09:08:47
【问题描述】:

想要水平和垂直对齐我的 JTextPane 的内容。我在这里找到了一个很好的教程,效果很好:http://java-sl.com/tip_center_vertically.html

但是,如果我想嵌入 HTML 代码,例如

doc.insertString(0,"<html> some text <b> bold text </b> </html>", attrs)

文本不会被呈现为 HTML,而是被视为简单文本。

关于如何解决这个问题的一些想法?

【问题讨论】:

    标签: java html swing jtextpane


    【解决方案1】:

    试试这样的

    HTMLDocument doc = (HTMLDocument)textPane.getDocument();
    HTMLEditorKit editorKit = (HTMLEditorKit)textPane.getEditorKit();
    String text = "<a href=\"abc\">hyperlink</a>";
    editorKit.insertHTML(doc, doc.getLength(), text, 0, 0, null);
    

    而且你不需要在里面添加&lt;html /&gt; 标签。

    【讨论】:

    • 抱歉,我无法正常工作。如果我按照你的方式做,布局将不再垂直居中文本
    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 2015-11-01
    • 2014-08-15
    • 2015-12-04
    • 2012-08-22
    • 1970-01-01
    • 2014-08-10
    • 2011-11-09
    相关资源
    最近更新 更多