在 JEditorPane 中使用 HTML 格式化文本?

发布于 2024-09-15 07:36:14 字数 351 浏览 3 评论 0原文

我正在尝试用 Java Swing 制作一个简单的电子邮件客户端。

我希望允许用户以任何他们想要的方式格式化他们的电子邮件,例如将文本的某些部分设为粗体,将其他部分设为斜体等。换句话说,我正在尝试制作一个所见即所得的编辑器。格式化是在 HTML 中完成的。我正在使用 JEditorPane 来显示文本。

我尝试使用 JEditorPane 的 setText 和 getText 方法直接将标签添加到文本中。我可以使它适用于基本格式,但处理复杂格式相当困难。 (例如,尝试从多标记元素中删除标记)

是否有更简单的方法来实现此目的?我看过 HTMLEditorKit 但它似乎不支持添加标签和/或替换特定字符串。

提前致谢。

I am trying to make a simple email client in 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. I am using JEditorPane to display the text.

I have tried adding tags myself to the text directly by using setText and getText methods of JEditorPane. 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.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞花火° 2024-09-22 07:36:14

HTMLEditorKit 附带了一些默认操作,允许您通过单击菜单项(或按钮)对文本进行一些基本样式设置。查看 Swing 教程 部分中的示例文本组件功能

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). Take a look at the example in the section from the Swing tutorial on Text Component Features.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文