首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JEditorPane,HTMLEditorKit返回换行符而不是<br>标记

JEditorPane,HTMLEditorKit返回换行符而不是<br>标记
EN

Stack Overflow用户
提问于 2014-11-09 03:38:38
回答 1查看 915关注 0票数 1

在使用安装了JEditorPane.getText()的JEditorPane.getText()时,我试图解决一个不一致的问题。

我可以使用JEditorPane.setText传递包含< br>标记的HTML,并且当我使用getText()时,这些新行正确地显示为< br>。但是当用户在JEditorPane中输入一个新行时,getText()返回一个“/n”字符,而不是一个< br>标记。我的自定义HTML解析器无法区分用户“/n”字符和添加了-seemingly的“/n”字符--以使HTML看起来很漂亮。举个例子:

如果用户输入某种文本,JEditorPane.getText()过程将返回如下内容:

代码语言:javascript
运行
复制
<html>
  <head>

  </head>
  <body>
    I've written some text! Indeed so much text that this line is probably 
    going to word wrap when I run the getText procedure!

And now I just hit enter a few times! I wonder what will happen if it wraps 
    another time? WHAM.
And I'll hit enter once more for good measure.
  </body>
</html>

然而,我希望这会显示为:

代码语言:javascript
运行
复制
<html>
  <head>

  </head>
  <body>
    I've written some text! Indeed so much text that this line is probably 
    going to word wrap when I run the getText procedure!<br><br>And now I 
    just hit enter a few times! I wonder what will happen if it wraps 
    another time? WHAM.<br>And I'll hit enter once more for good measure.
  </body>
</html>

当用户点击enter时,是否有任何方式将< br>插入到getText字符串中?我的第一次尝试是使用documentFilter,但是文档说我只是大声地在过滤器中使用insertString或filterBypass,因此我不能使用setText (“ ”)路由。在大量阅读之后,我想另一个选择是扩展HTMLEditorKit并重写读取过程?JTextComponents对我来说是新的,所以这已经超出了我的想象。还有其他选择吗?还是资源?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2014-11-10 09:30:42

您可以使用DocumentListener并跟踪\n插入。在insert上,为插入的\n创建一个虚拟元素,并替换它的外部html (使用HTMLDocument的setOuterElement()方法)。

参见自动替换smiles 这里的示例

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26824725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档