当我将样式化的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);
最佳答案
默认的RTFEditorKit确实很有限。用http://java-sl.com/advanced_rtf_editor_kit.html尝试相同的操作
关于java - Java-另存为RTF不支持文本对齐?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11423293/