当我将样式化的 JTextPane 保存为 RTF,然后重新打开它时,不会保留文本对齐方式。这是我的方法:
private void saveAsRTF(File outfile) {
RTFEditorKit rtfkit = new RTFEditorKit();
StyledDocument doc = (StyledDocument) pane.getDocument();
try {
FileOutputStream fwi = new FileOutputStream(outfile);
rtfkit.write(fwi, doc, 0, doc.getEndPosition().getOffset());
fwi.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (BadLocationException ble) {
ble.printStackTrace();
}
}
和(打开 RTF)
RTFEditorKit rtf = new RTFEditorKit();
FileInputStream fi = new FileInputStream(j.getSelectedFile());
rtf.read(fi, pane.getStyledDocument(), 0);
最后,首先对齐文本:
SimpleAttributeSet attribs = new SimpleAttributeSet();
StyleConstants.setAlignment(attribs , StyleConstants.ALIGN_CENTER);
pane.setParagraphAttributes(attribs,true);