[英]How to create a custom Swing Document structure
我有一个JEditorPane
包含一个自定义EditorKit
和一个自定义Document
(从DefaultStyledDocument派生)。
以下是JEditorPane
内容的示例:
第一段
第二段
对于上面的示例,我得到一个具有以下等效于XML的文档结构:
<root>
<section>
<paragraph>
<content>first</content>
<content bold="true">paragraph</content>
</paragraph>
<paragraph>
<content>second paragraph</content>
<content>\n</content>
</paragraph>
</section>
</root>
请注意,上面的标签名称由Element.getName()函数确定。
我的意图是通过自定义元素类型扩展此结构以编辑除样式文本之外的内容。
一个示例是将编辑器扩展为音乐注释编辑器,以获得如下所示的XML结构:
<root>
<section>
<paragraph>
<content>first</content>
<content bold="true">paragraph</content>
</paragraph>
<musicnotes>
<bar>
<note>C</note>
<note>D</note>
<note>E</note>
</bar>
</musicnotes>
</section>
</root>
如我所见,样式元素和段落元素是根据Document.insertString()和Document.setCharacterAttributes()方法创建的。
我的问题是我不知道如何重写这些方法(或编写吊坠),以不回到默认结构而是使用自定义元素种类。
我什至不知道这是否是正确的方法。 我是否必须创建自己的Document-interface实现来创建自定义文档结构?
请参阅表创建示例。 http://java-sl.com/JEditorPaneTables.html
您可以使用相同的定义所需结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.