JEdi​​torPane从图像链接中删除边框

时间:2014-02-16 22:45:35

标签: java swing hyperlink border jeditorpane

我有一个JEditorPane,它显示一个包含如下图像的链接:

<a href='http://somesite.com/'>
    <img src='someImage.png' />
</a>

当JEditorPane将其显示为HTML时,它会在图像周围放置一个蓝色边框,我试图将其移除而没有任何运气。

我希望它在jeditorpane中看起来像这样: image:(http://randomcloud.net/img/prob/valid.png

但这就是JEditorPane显示它的方式: 图像(http://randomcloud.net/img/prob/jeditorpane.png

这是我到目前为止所尝试过的,它仍然无法正常工作

editorPane = new JEditorPane("http://randomcloud.net/ads/index.php?id=1");
StyleSheet style = ((HTMLDocument)editorPane.getDocument()).getStyleSheet();
style.addRule("a img {text-decoration: none; border: none;}");

有任何建议或见解吗?

-Michel

2 个答案:

答案 0 :(得分:1)

HTLEditorKit的ImageView源码。如您所见,borderSize设置为DEFAULT_BORDER(2个像素)。您可以在ViewFactory的实现中替换ImageView创建并覆盖该方法以提供所需的边框(我理解为0)。

protected void setPropertiesFromAttributes() {
    StyleSheet sheet = getStyleSheet();
    this.attr = sheet.getViewAttributes(this);

    // Gutters
    borderSize = (short)getIntAttr(HTML.Attribute.BORDER, isLink() ?
                                   DEFAULT_BORDER : 0);

    leftInset = rightInset = (short)(getIntAttr(HTML.Attribute.HSPACE,
                                                0) + borderSize);
    topInset = bottomInset = (short)(getIntAttr(HTML.Attribute.VSPACE,
                                                0) + borderSize);

    borderColor = ((StyledDocument)getDocument()).getForeground
                  (getAttributes());

    AttributeSet attr = getElement().getAttributes();

    // Alignment.
    // PENDING: This needs to be changed to support the CSS versions
    // when conversion from ALIGN to VERTICAL_ALIGN is complete.
    Object alignment = attr.getAttribute(HTML.Attribute.ALIGN);

    vAlign = 1.0f;
    if (alignment != null) {
        alignment = alignment.toString();
        if ("top".equals(alignment)) {
            vAlign = 0f;
        }
        else if ("middle".equals(alignment)) {
            vAlign = .5f;
        }
    }

    AttributeSet anchorAttr = (AttributeSet)attr.getAttribute(HTML.Tag.A);
    if (anchorAttr != null && anchorAttr.isDefined
        (HTML.Attribute.HREF)) {
        synchronized(this) {
            state |= LINK_FLAG;
        }
    }
    else {
        synchronized(this) {
            state = (state | LINK_FLAG) ^ LINK_FLAG;
        }
    }
}

我认为蓝色边框只是选择文字。尝试取消选择内容或使用jEditorPaneInstance.getCaret().setSelectionVisible(false);

答案 1 :(得分:1)

@ Alien595:在 img 标记上,您可以添加名为 border 的属性为0.

示例:

<a href="your_link.html">
    <img border="0" src="your_image.png"/>
</a>