我可以这样做,我的富文本将具有固定的宽度
JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setText("<html><body style='width:100px'>Some text</body></html>"); 但是我直接处理文档,而不是HTML。如何设置宽度?
DefaultStyledDocument doc = (DefaultStyledDocument) pane.getStyledDocument();
//??? (set width)发布于 2015-05-08 17:02:58
宽度不是模型(文档)的一部分。应该有一个View来处理文档中的width属性,并相应地设置宽度。
您应该替换EditorKit的ViewFactory中的根视图(SectionView),以返回宽度取自文档元素的自定义视图。
例如,请参阅链接http://java-sl.com/Pagination_In_JEditorPane.html
它描述了如何向EditorKit添加分页。您可以使用代码作为示例,并且仅固定宽度。
Extend EditorKit.
Replace default ViewFactory with yours
Replace root view (SectionView)
Replace the view's getPreferredSpan(int axis) for X axis to use the width from the Document发布于 2015-05-08 17:13:28
除了文档是StyledDocument之外,它也只有两个层次:段落和字符。在这种情况下,可以使用HTMLDocument并使用CSS样式表(body { width: 14cm; })构造它,或者适当地修改CSS样式表。
https://stackoverflow.com/questions/30096588
复制相似问题