gpt4 book ai didi

java - 如何添加到 Jtextpane 中的现有 HTML

转载 作者:行者123 更新时间:2023-12-01 11:12:01 27 4
gpt4 key购买 nike

我正在用 Java/Swing 制作一个聊天程序,文本在 Jtextpane 对象中呈现。现在,一条新消息会删除旧消息,因为我不知道如何添加到现有文档中。如何做到这一点?

public void addMessage(String sender, String msg) throws BadLocationException, IOException{
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();
pane.setEditorKit(kit);
pane.setDocument(doc);

kit.insertHTML(doc, doc.getLength(), "<b>[" + sender + "]</b> " + msg, 0, 0, null);

}

最佳答案

不要使用 HTML。

只需使用常规文本,然后您可以将文本与样式属性相关联。

例如:

//  create a set of attributes

Simple AttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);

// Add some text

try
{
StyledDocument doc = textPane.getStyledDocument();
doc.insertString(doc.getLength(), "\nEnd of text", keyWord );
}
catch(Exception e) {}

关于java - 如何添加到 Jtextpane 中的现有 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32234501/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com