从Jar加载Swing HTML的图像 [英] Loading images from jars for Swing HTML
问题描述
虽然这个答案适用于从Jar加载图片文件为 ImageIcons
,我似乎无法获得 Swing HTML 。
While this answer works to load images from Jar files for ImageIcons
, I cannot seem to get the right path for images referenced in Swing HTML.
当资源未绑定到Swing HTML中时, a jar:
This displays an image in the Swing HTML when the resources are not bundled into a jar:
new JLabel("<html><table cellpadding=0><tr><td><img src='file:icons/folder_link.png'></td></tr><tr><td>100</td></tr></table></html>") );
在jar内部,图像可以被成功引用(并显示)为 ImageIcon
:
Inside of the jar, the image can be successfully referenced (and displayed) into an ImageIcon
:
Icon topIcon = new ImageIcon( getClass().getResource("icons/folder_link.png" ) );
但是,我尝试使用 getResource
技术为Swing HTML不起作用。
However, my attempt to use the getResource
technique for Swing HTML doesn't work.
String p = getClass().getResource("icons/folder_link.png" ).getPath();
new JLabel("<html><table cellpadding=0><tr><td><img src='" + p + "'></td></tr><tr><td>100</td></tr></table></html>") );
秘诀是什么?
What's the secret?
推荐答案
如果您在HTML代码中包含资源网址,则假设HTML渲染器可以访问您的图像:
Without actually having tried it, I would assume that the HTML renderer can access your image if you include the resource URL in your HTML code:
String p = getClass().getResource("icons/folder_link.png" ).toString();
new JLabel("<html><table cellpadding=0><tr><td><img src='" + p + "'></td></tr><tr><td>100</td></tr></table></html>") );
这篇关于从Jar加载Swing HTML的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!