作为使用javax.swing学习Java GUI编程的一部分,我打算用字体保存JTextPane的内容,有什么办法吗?
是的,你可以使用HTMLEditorKit或RTFEditorKit来保存JTextPane的内容。
StyledDocument doc = (StyledDocument)textPane.getDocument();
HTMLEditorKit kit = new HTMLEditorKit();
BufferedOutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream(new File("rich.html")));
kit.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength());
} catch (FileNotFoundException | IOException e) {
} catch (BadLocationException e) {
}