我正在用 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);
}