如何在 Swing 的 JTextPane 中删除链接标签内图像周围的边框? [英] How to remove border around image inside link tag in Swing's JTextPane?

查看:31
本文介绍了如何在 Swing 的 JTextPane 中删除链接标签内图像周围的边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JTextPane 中使用以下 HTML 代码时:

只是一张图片:<img src="image.png"/><br/><a href="https://example.net">一个链接</a><br/>图片内链接:<a href="https://example.net"><img src="image.png"></a>

你会得到以下结果:

我添加了一个 CSS 规则来使链接颜色变为红色:

StyleSheet css = htmlEditorKit.getStyleSheet();css.addRule("a { color: red; }");

正如您在屏幕截图中看到的那样,这也将图像框架变为红色,由此我得出结论,是

When using following HTML code inside JTextPane:

Just an image: <img src="image.png" /><br/>
<a href="https://example.net">A link</a><br/>Image inside link:
<a href="https://example.net"><img src="image.png"></a>

You'll get the following result:

I've added a single CSS rule to make the link color red:

StyleSheet css = htmlEditorKit.getStyleSheet();
css.addRule("a { color: red; }");

As you can see on the screenshot, this turned the image frame red as well, from which I conclude that it is the <a> tag that is causing the border to appear.


How to get rid of this border around image inside <a> tag?

I've tried overriding border with another rule, but it did not work:

css.addRule("img { border: 0; }");

解决方案

Looking at code of HTMLEditorKit, you can see that <img> tag is rendered using class ImageView, which contains following code:

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

and DEFAULT_BORDER is hardcoded to 2.

Note, that it checks an HTML attribute. If HTML attribute border is not specified, then the border of images is 2 when inside a link, and zero otherwise.

A solution would be to specify border directly inside the <img> tag:

Image inside link:
<a href="https://example.net"><img src="image.png" border="0"></a>

这篇关于如何在 Swing 的 JTextPane 中删除链接标签内图像周围的边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆