JTextPane和HTML [英] JTextPane and HTML

查看:492
本文介绍了JTextPane和HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个文本编辑器,就像一个微软的单词,我正在使用带有HTML的文本窗格。在做了一些研究之后,我找到了一种方法,将html应用于使用StyleConstants的突出显示部分,然后是一个JTextPane.getStyledDocument()。setCharacterAttributes();的方法。问题是,当我使用textpane中的getText()方法时,缺少样式化文本。例如,我将一个普通的字符串输入到textpane中,getText()返回html代码和文本。当我设置
文本并使用getText()时,所有样式的单词都会被删除,并保留正常的单词。

I am making a text editor roughly like a microsoft word and I am using a textpane with HTML. After doing some research I found a way to apply html to a highlighted section on using StyleConstants and then a method that goes JTextPane.getStyledDocument().setCharacterAttributes();. Problem is that when I use the getText() method from the textpane, the styled text is missing. For example I input a normal string into the textpane, the getText() returns the html code and the text. When I style the text and use getText() all the styled words are deleted and normal words are preserved.

我想要完成的是,将整个纯文本(包括html)保存在文本文件中,当我将其加载回来并将其设置在JTextPane上时,它保留了它的样式,但由于某些原因,在getText()方法中删除了样式文本正常文本保存正常。谢谢

What I'm trying to accomplish is, to save the entire plain text (including the html) on a text file and when I load it back up and set it on the JTextPane, it keeps its style, but for some reason styled text is deleted on the "getText()" method and normal text is preserved normally. Thanks

推荐答案

您可以按照此处的描述实现自己的自定义读取器/写入器 http://java-sl.com/editor_kit_tutorial_reader_writer.html

You can implement your own custom Reader/Writer as described here http://java-sl.com/editor_kit_tutorial_reader_writer.html

当你使用setCharacterAttributes时存储在叶子元素属性中的内容,以便您可以访问它们并按照您的意愿存储。

When you use setCharacterAttributes the content stored in leaves Elements attributes so you can access them and store as you wish.

这篇关于JTextPane和HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆