swing 创建一个DefaultStyledDocument

示例

try {
    StyledDocument doc = new DefaultStyledDocument();
    doc.insertString(0, "This is the beginning text", null);
    doc.insertString(doc.getLength(), "\nInserting new line at end of doc", null);
    MutableAttributeSet attrs = new SimpleAttributeSet();
    StyleConstants.setBold(attrs, true);
    doc.insertString(5, "This is bold text after 'this'", attrs);
} catch (BadLocationException ex) {
    //处理错误
}

DefaultStyledDocuments可能是您使用最多的资源。可以直接创建它们,并StyledDocument抽象类的子类。