Java HTMLEditorKit返回换行符而不是<;的JEditorPane;br>;标签
我试图解决在安装了HTMLEditorKit的情况下使用JEditorPane.getText()时出现的不一致问题 我可以使用JEditorPane.setText传递包含Java HTMLEditorKit返回换行符而不是<;的JEditorPane;br>;标签,java,jeditorpane,documentfilter,htmleditorkit,Java,Jeditorpane,Documentfilter,Htmleditorkit,我试图解决在安装了HTMLEditorKit的情况下使用JEditorPane.getText()时出现的不一致问题 我可以使用JEditorPane.setText传递包含标记的HTML字符串,当我使用getText()时,这些新行正确显示为。但是,当用户在JEditorPane中输入新行时,getText()将返回一个“/n”字符,而不是一个标记。我的自定义HTML解析器无法区分用户“/n”字符和添加的“/n”字符(似乎是为了使HTML字符串看起来漂亮)。例如: 如果用户输入一些文本,JEd
标记的HTML字符串,当我使用getText()时,这些新行正确显示为
。但是,当用户在JEditorPane中输入新行时,getText()将返回一个“/n”字符,而不是一个
标记。我的自定义HTML解析器无法区分用户“/n”字符和添加的“/n”字符(似乎是为了使HTML字符串看起来漂亮)。例如: 如果用户输入一些文本,JEditorPane.getText()过程将返回如下内容:
<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>
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();
editor_pane.setEditorKit(kit);
editor_pane.setDocument(doc);
我写了一些文字!事实上,这么多的文字,这一行可能是
当我运行getText过程时,将转到word wrap!
现在我只按了几下回车键!我不知道如果它结束了会发生什么
下次砰
我会再按一下回车键。
而我希望这会表现为:
<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>
我写了一些文字!事实上,这么多的文字,这一行可能是
当我运行getText过程时,将转到word wrap<现在我
只需按几下回车键!我不知道如果它结束了会发生什么
下次嗯。
我再按一下回车键。
当用户点击enter键时,是否有方法将插入getText字符串?我的第一次尝试是使用documentFilter,但文档中说我只想在筛选器中使用insertString或filterBypass,因此我不能使用setText(
)路由。经过大量的阅读,我想另一个选择是扩展HTMLEditorKit并覆盖阅读过程?JTextComponents对我来说是新的,所以这是我所无法理解的。还有其他选择吗?还是资源
谢谢 您可以使用DocumentListener并跟踪\n插入。在insert上,为插入的元素创建一个伪元素\n并替换它的外部html(使用HTMLDocument的setOutRelation()方法) 参见自动替换微笑的示例明白了 我的初始化如下所示:
<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>
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();
editor_pane.setEditorKit(kit);
editor_pane.setDocument(doc);
但这似乎不足以让文档处理所有用户输入。不幸的是,正确处理StyledEditorKit.BoldAction或StyledEditorKit.italication就足够了,这就是为什么我认为问题不在初始化中
HTMLEditorKit kit = new HTMLEditorKit();
editor_pane.setEditorKit(kit);
editor_pane.setContentType("text/html");
按照@StanislavL shared一文中的建议,我将初始化更改为上述内容。通过此更正,当用户点击enter键时,JEditorPane现在会创建新的段落,这对于我来说已经足够好了。谢谢你的帮助 你能发布SSCCE来说明这个问题吗?