1

当我将样式化的 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);
4

2 回答 2

2

默认的 RTFEditorKit 确实是有限的。尝试与http://java-sl.com/advanced_rtf_editor_kit.html相同

于 2012-07-11T05:45:19.823 回答
0

已针对 Java 9 修复 RTFEditorKit 对齐。
https://bugs.openjdk.java.net/browse/JDK-8139215

于 2016-05-06T07:19:58.340 回答