JTextPane + JScrollPane + 自动换行?

Cos*_*ole 5 java swing 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);
Run Code Online (Sandbox Code Playgroud)

WrapEditorKit 是此处可用的。我也尝试过 ScrollablePanel,但每次 JTextPane 的文本都没有被包装。如何在只有垂直滚动(和水平字或强制换行)的 JScrollPane 中实现 JTextPane ?

Cos*_*ole 1

这显然是由<pre>标签而不是任何其他设置引起的。如果没有<pre>标签,文本似乎可以正确换行。我最初添加它们是为了防止换行符出现另一个问题,但我会尝试使用其他方法来修复它。如果使用一些更高级的 HTML 技能,这种情况永远不会发生。