本文介绍了从用于 Swing HTML 的 jar 加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

虽然 这个答案 可以从 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.

当资源未捆绑到 jar 中时,这会在 Swing HTML 中显示图像:

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

秘诀是什么?

推荐答案

在没有实际尝试的情况下,我假设如果您在 HTML 代码中包含资源 URL,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>") );

这篇关于从用于 Swing HTML 的 jar 加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 15:03