简体   繁体   English

如何使用样式格式获取JTextPane的内容

[英]how to get the content of a JTextPane with its styled format

I am trying to make a simple word processor that edits the text to make it bold, italic, underline, background color and foreground color. 我正在尝试制作一个简单的文字处理器来编辑文本,使其变为粗体,斜体,下划线,背景颜色和前景色。 The problem is I want to set the contents/text of the JTextPane with all its edited attributes to a single object to save it to another class as a data field which have other data fields like date created and the name of the document given by the user. 问题是我想将JTextPane的内容/文本及其所有已编辑的属性设置为单个对象,以将其作为数据字段保存到另一个类,该数据字段具有其他数据字段,如创建日期和文档名称由用户。

I think the best approach its using html as content type for the Text Pane and string builders. 我认为最好的方法是使用html作为文本窗格和字符串构建器的内容类型。

for example, 例如,

TextPane tp = new JTextPane();
tp.setContentType("text/html");
StringBuilder sb = new StringBuilder();
sb.append("<span style=\"color:red\">"  + Hello red  + "</span>");
sb.append("<span style=\"color:blue\">" + Hello blue + "</span>");
...
tp.setText(sb); // will print text with the style

works the same in the other way, 以相反的方式工作,

String txt = tp.getText();
System.print(txt); //wil show html code

You can reference http://www.java2s.com/Tutorials/Java/Swing_How_to/JTextPane/Style_JTextPane_with_HTML_and_CSS.htm 您可以参考http://www.java2s.com/Tutorials/Java/Swing_How_to/JTextPane/Style_JTextPane_with_HTML_and_CSS.htm

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

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