根据现有解决方案,我还没有找到使 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 是可用的 here .我也试过 ScrollablePanel,但每次 JTextPane 的文本都没有换行。如何在只有垂直滚动(以及水平字或强制换行)的 JScrollPane 中实现 JTextPane?
最佳答案
这显然是由 <pre>
引起的标签而不是任何其他设置。没有 <pre>
标签,文本似乎可以正确换行。我最初添加它们是为了防止换行符出现另一个问题,但我会尝试使用其他方法修复它。使用一些更高级的 HTML 技能永远不会发生这种情况。
关于java - JTextPane + JScrollPane + 自动换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27451491/