簡體   English   中英

如何創建自定義的Swing文檔結構

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM