2

我有一个基于 HTML 的 jEditorPane。我发现你可以使用:

String text = jEditorPane1.getDocument().getText(0, jEditorPane1.getDocument().getLength());
text = text.replaceAll("(?<!^)\n", "\n<br />");
jEditorPane1.setText("<html>" + text + "</html>");

通过这种方式,我从编辑器窗格中获取了文本。当我想把它放回编辑器窗格时,我只需用 < br /> 替换换行符。然后把它放回去。

直到现在一切都很好。但是当我第二次从编辑器窗格中获取文本时,没有换行符。

我怎样才能保留这个换行符?

4

2 回答 2

2

尝试使用"</p><p>"而不是"<br>"在中间和

jEditorPane1.setText("<html><p>" + text + "</p></html>");
于 2011-08-13T13:59:07.183 回答
1
<p style=\"margin-top: 0\">

这为我完成了工作。感谢斯坦尼斯拉夫尔!我已经检查了换行符在 html 模式下的 jeditpane 中的正常工作方式。它是没有边距的 p 标签。奇怪的暗示,但是它起作用了。

text = text.replaceAll("\n(.*?)(?=(\n|$))", "<p style=\"margin-top: 0\">$1</p>");

这是真正的工作。它用 < p>< /p> 标签包围 \n 之后的所有文本。

谢谢你的帮助

于 2011-08-13T21:07:37.693 回答