0

我在 Swing 的 JTextPane 中搞乱了 HTMLDocument。如果我有这种情况:

 <html>... 
    <p id='paragraph1'><span>something</span></p> 
    <span id='span1'><span>something else</span></span>
 ...</html>

(额外的<span>标签是为了防止Swing抱怨我不能改变叶子的innerHTML)或者这种情况

 <html>... 
    <p id='paragraph1' />
    <span id='span1' />
 ...</html>

我可以调用 HTMLDocument.getElement() 并找到 ID 为“paragraph1”的元素,但找不到 ID 为“span1”的元素。如果我将“span1”的标签从“span”更改为“p”,那么我很好。WTF在这里进行吗?是否有另一个我可以使用的 HTML 元素,它允许我使用 id 属性访问文档的特定部分,这不会导致换行符?(跨度本来是完美的:(啊!)

编辑:我认为解决方案是重新检查我正在尝试做的事情,这是利用我知道如何在 HTML 中制作 GUI + 表格 + 显示的事实比我在 Swing 中所做的要多得多,所以我会问一个不同的问题....

4

3 回答 3

1

我不知道摇摆,但是

<p style="display: inline;">不换行,同<span>

于 2009-04-17T16:26:39.137 回答
1

我正好有这个问题。我的跨度元素消失了。而如果我使用 div 我可以看到它们。但我当然不想要 div 元素,因为它会导致换行。

该死的!该死的爪哇。

编辑!

停止新闻!

找到了答案。至少,一个为我解决问题的答案。

我仍然能够确定我有我的 span 元素。我将描述我在做什么,并提供代码来说明我是如何做到的。

我想知道插入符号在哪个元素中。因此,此代码存在于 caretUpdate 函数中,它每次移动时都会为我提供插入符号的位置。

@Override
public void caretUpdate(CaretEvent e)
{
    System.out.println("caret event: " + e.toString());
    Object source = e.getSource();

    if (source instanceof JEditorPane)
    {
        JEditorPane jep = (JEditorPane)source;
        Document doc = jep.getDocument();
        if (doc instanceof HTMLDocument)
        {
            HTMLDocument hdoc = (HTMLDocument)doc;
            int pos = e.getDot();
            Element elem = hdoc.getCharacterElement(pos);
            AttributeSet a = elem.getAttributes();
            AttributeSet spanAttributeSet = (AttributeSet)a.getAttribute(HTML.Tag.SPAN);

            // if spanAttributeSet is not null, then we properly found ' a span '.
            // now we need to discover if it is one of OUR spans
            if (spanAttributeSet!=null)
            {
                Object type = spanAttributeSet.getAttribute(HTML.Attribute.TYPE);
                if (type !=null && type.equals("dragObject"))
                {
                    // for our logging, we get the ref, which holds the source
                    // of our value later
                    System.out.println("the value is: " + spanAttributeSet.getAttribute("ref"));
                }                   
            }
        }
    }
}

编辑!!!

从头开始...这几乎可以工作...除了 Sun 的白痴决定密钥将是 HTML.Attribute 类型。不仅如此,HTML.Attribute 的构造函数是私有的,而且恰好我想要的属性类型不存在于他们的特权属性集中。混蛋!所以,一切都没有丢失......我仍然可以通过枚举器得到它......但这比它需要的要困难一些。

最后编辑!

好的,我现在明白了。如果属性是已知类型,则将其作为 HTML.Attribute("type") 的实例存储在 AttributeSet 中。否则,它以“String”作为键存储在 AttributeSet 中。愚蠢的。但我已经到了。

于 2009-07-16T01:17:28.023 回答
0

我查看了HTMLDocument的 javadoc ,它指向了HTMLReader

我没有在 HTMLReader 中看到任何关于 span 的提及。也许它只是不知道那个元素。

P 可能不是 span 的好替代品。P 是块级元素,span 是文本级元素(参见这些术语的描述)。也许尝试没有属性的字体(另一个文本级元素)?

于 2009-04-17T16:37:45.960 回答