HTMLDocument:Swing 是否“优化”了 跨度元素?

发布于 2024-07-17 10:18:21 字数 808 浏览 5 评论 0原文

我正在摆弄 Swing 中 JTextPane 中的 HTMLDocument。 如果我遇到这种情况:(

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

额外的 标签是为了防止 Swing 抱怨我无法更改叶子的innerHTML)或者这种情况

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

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

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

I'm messing about with HTMLDocument in a JTextPane in Swing. If I have this situation:

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

(the extra <span> tags are to prevent Swing from complaining that I can't change the innerHTML of a leaf) or this situation

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

I can call HTMLDocument.getElement() and find the element with ID 'paragraph1' but not the element with id 'span1'. If I change the tag for 'span1' from "span" to "p" then I'm fine. WTF is going on here? Is there another HTML element I can use instead that will allow me to access a particular portion of the document using the id attribute, that will not cause linebreaks? (span would have been perfect :( argh!)

edit: I think the solution is to re-examine what I'm trying to do, which was to leverage the fact that I know how to make GUIs + tables + displays in HTML a lot more than I do in Swing, so I'll ask a different question....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖树树初阳… 2024-07-24 10:18:21

我不懂Swing,但是

不会换行,和一样

I don't know Swing, but

<p style="display: inline;"> does not line-break, the same as <span>

囍笑 2024-07-24 10:18:21

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

该死的! 该死的爪哇。

编辑!

停止媒体!!

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

我仍然能够确定我有我的跨度元素。 我将描述我正在做什么,并提供我如何做到这一点的代码。

我想知道插入符位于哪个元素中。因此,此代码存在于 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 中。
愚蠢的。 但我已经到了那里。

I have exactly this problem. My span elements disappear.
Whereas if i used div i can see them. But of course I don't want div elements, because it causes a line break.

Damn it! Damn java.

Edit!

STOP THE PRESS!!

Found the answer. At least, an answer which fixes it for me.

I was still able to determine that I had my span element. I will describe what I am doing, an d provide the code to how i did it.

I want to know what element the caret is in. So, this code exists within the caretUpdate function, which provides me with the caret position each time it moves.

@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"));
                }                   
            }
        }
    }
}

Edit!!!

Scratch that... This almost works... except the idiots at Sun decided the key was going to be of type HTML.Attribute. Not only that, that the constructor for the HTML.Attribute is private, and it just so happens that the attribute type that I wanted doesn't exist within their privileged set of attributes. Bastards!
So, all is not lost... I can still get it via the enumerator.. but it is a little more difficult than it needed to be.

LAST EDIT!

Ok, I get it now. If the attribute is of a known type, it is stored in the AttributeSet as an instance of HTML.Attribute("type").
Otherwise, it is stored in the AttributeSet with a 'String' as the key.
Stupid. But i've got there.

庆幸我还是我 2024-07-24 10:18:21

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

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

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

I took a look at the javadoc for HTMLDocument, which pointed me to HTMLReader.

I don't see any mention of span in HTMLReader. Maybe it just doesn't know that element.

P probably isn't a good replacement for span. P is a block-level element and span is a text-level element (see description of those terms). Maybe try font (another text-level element) with no attributes?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文