简体   繁体   English

在JEditorPane中用HTML格式化文本?

[英]Formatting text with HTML in a JEditorPane?

I am trying to make a simple email client in Java Swing. 我试图在Java Swing中创建一个简单的电子邮件客户端。

I want to allow users to format their email in any way they want, like making some parts of the text bold, other parts italic, etc. In other words, I am trying to make a WYSIWYG editor. 我想允许用户以他们想要的任何方式设置电子邮件格式,例如使文本的某些部分变为粗体,其他部分变为斜体等。换句话说,我正在尝试制作所见即所得的编辑器。 The formatting is done in HTML. 格式以HTML格式完成。 I am using JEditorPane to display the text. 我正在使用JEditorPane来显示文本。

I have tried adding tags myself to the text directly by using setText and getText methods of JEditorPane. 我尝试使用JEditorPane的setText和getText方法直接将标签自己添加到文本中。 I could make it work for basic formatting, but it is quite difficult to handle complex formatting. 我可以使其适用于基本格式,但是处理复杂的格式非常困难。 (trying to remove tags from multi-tagged elements, for example) (例如,尝试从多标签元素中删除标签)

Is there an easier way to accomplish this? 有没有更简单的方法可以做到这一点? I have looked at HTMLEditorKit but it seems like it does not support adding tags to and/or replacing a specific string. 我看过HTMLEditorKit,但似乎它不支持在标签和/或替换特定字符串中添加标签。

Thanks in advance. 提前致谢。

The HTMLEditorKit comes with some default Actions that allow you to do some basic styling of the text with the click of a menu item (or button). HTMLEditorKit带有一些默认的动作,这些动作使您可以通过单击菜单项(或按钮)来对文本进行一些基本的样式设置。 Take a look at the example in the section from the Swing tutorial on Text Component Features . 看一下Swing教程“ 文本组件功能 ”部分中的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM