簡體   English   中英

設置StyledDocument(JTextPane)的正文寬度

[英]Set body width for StyledDocument (JTextPane)

我可以這樣做,並且我的RTF文本將具有固定的寬度

JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setText("<html><body style='width:100px'>Some text</body></html>"); 

但是我直接使用Document,而沒有HTML。 如何設置寬度?

DefaultStyledDocument doc = (DefaultStyledDocument) pane.getStyledDocument();
//??? (set width)

寬度不是模型(文檔)的一部分。 應該有一個視圖來處理Document的width屬性,並相應地設置寬度。

您應該替換EditorKit的ViewFactory中的根視圖(SectionView)以返回具有從Document元素獲取的寬度的自定義視圖。

例如,請參閱鏈接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

除了Document之外,還有StyledDocument,它也只有兩個級別:段落和字符。 在這種情況下,可以使用HTMLDocument並使用CSS樣式表( body { width: 14cm; } )構造它,或適當地更改CSS樣式表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM