当我将样式化的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);
答案 0 :(得分:2)
默认RTFEditorKit非常有限。使用http://java-sl.com/advanced_rtf_editor_kit.html
尝试相同的操作答案 1 :(得分:0)
针对Java 9修复了RTFEditorKit对齐 https://bugs.openjdk.java.net/browse/JDK-8139215