[英]JTextPane + JScrollPane + word wrap?
基於現有的解決方案,我還沒有找到一種方法來使 JTextPane(或 JEditorPane)在 JScrollPane 內具有自動換行或強制換行。 JTextArea 不是解決方案,因為我需要在特定輸入中顯示 HTML。
output = new JTextPane();
scrollPane.setViewportView(output);
contentPane.add(scrollPane, gbc_scrollPane);
output.setEditorKit(new WrapEditorKit());
output.setForeground(Color.WHITE);
output.setFont(new Font("Courier New", Font.PLAIN, 12));
output.setEditable(false);
output.setContentType("text/html; charset=UTF-8");
output.setBackground(Color.DARK_GRAY);
WrapEditorKit 是此處可用的。 我也嘗試過 ScrollablePanel,但每次 JTextPane 的文本都沒有被包裝。 如何在只有垂直滾動(和水平字或強制換行)的 JScrollPane 中實現 JTextPane ?
這顯然是由<pre>
標簽而不是任何其他設置引起的。 如果沒有<pre>
標簽,文本似乎可以正確換行。 我最初添加它們是為了防止換行出現另一個問題,但我會嘗試使用其他方法修復它。 使用一些更高級的 HTML 技能,這是不可能發生的。
大約 6 年后,我遇到了同樣的問題。 我的不是由 <pre> 標簽引起的 - 文本中沒有它們。
解決方案:
子類JEditorPane
或( JTextPane
是JEditorPane
的子類)並覆蓋boolean javax.swing.Scrollable#getScrollableTracksViewportWidth()
。
從此方法返回 true 會強制Scrollable
(例如JTextPane
)包裝其內容,而不是在包含JScrollPane
下激活水平滾動。
根據文檔應該與所有Scrollable
: DefaultTreeCellEditor.DefaultTextField, JEditorPane, JFormattedTextField, JLayer, JList, JPasswordField, JTable, JTextArea, JTextComponent, JTextField, JTextPane, JTree 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.