JLabel可以有img标签吗
我正在尝试显示一个 JLabel,它有几行文本和一个图像,如下所示:
String html = "<html> hello </br> <img src = \"/absolute/path/here\" height = \"30\" width =\"40\"/> </html>";
JLabel l = new JLabel(html);
对于我得到的图像,我得到的只是一个损坏的图像,是否可以在 JLabel 内嵌套 img 标签?
编辑: 我想向 JLabel 添加多个图像,因此我认为此处不能使用 ImageIcon。
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这对我有用。它很简单,可以放置任意数量的图像,而不仅仅是一个图像图标。如果没有引号,它就不起作用。
This works for me. It's simple and gives possibility to put any number of images you want, not just one image icon. It's not working without quotation marks.
对于图像,我得到的只是一个损坏的图像,是否可以将 img 标签嵌套在 JLabel 中
可以可以显示图像)在 JLabel 的文本中。您收到的图像已损坏,因为路径不正确。您需要在路径前添加
file:
前缀,或者最好让 java 使用class.getResource("/your/path")
为您完成此操作。这是一个工作示例,只需插入有效的资源路径。对于 Java 中更复杂的 HTML,我推荐 xhtmlrenderer。
For the image all I get is a broken image, is it possible to nest img tags inside a JLabel
It is possible to display image(s) in a JLabel's text. You are getting broken images because the path is not correct. You need to either prefix your path with
file:
or preferably get java to do it for you withclass.getResource("/your/path")
. Here is a working example, just insert valid resource paths.For more complex HTML in java, I recommend xhtmlrenderer.
除非您对 JEditorPane 感到满意,否则您基本上看到的是 Swing 内部的完整 Web 浏览器。
理想情况下,您可以使用 JWebPane,它是一个 WebKit 视图,作为 Swing 组件,但它还没有推出。我能找到的最新信息是这篇博客文章。
DJ 项目 允许在 Swing 中嵌入平台的本机浏览器。它在 Windows 上使用 Internet Explorer,在 Linux 上使用 XULRunner。它不支持 Mac。
Unless you're happy with JEditorPane, you're basically looking at a full webbrowser inside of Swing.
Ideally, you would use JWebPane which would be a WebKit view as a Swing component, but it isn't out yet. The most recent information I could find was this blog post.
The DJ project allows embedding the platform's native browser in Swing. It uses Internet Explorer on Windows and XULRunner on Linux. It does not have any support for Mac.
与其尝试在单个 JLabel 上拥有多个图像,为什么不简单地拥有多个 JLabel,每个 JLabel 都有一个图像(如 uthark 所描述的),然后将所有标签组合到一个 JPanel 上。这应该会给您带来您正在寻找的效果,并且只需要最小的额外复杂性。
Rather then try to have multiple images on a single JLabel why not simply have many JLabels, each with one image (as uthark described) and then group all the labels together on a single JPanel. That should give you the effect you are looking for with only minimal additional complexity.
使用 JEditorPane 显示 HTML。您可以更改背景、前景、字体等,使其看起来像一个标签。
Use a JEditorPane to display the HTML. You can change the background, forground, font etc so it looks like a label.
上面的方法似乎不再起作用了。
看来您现在必须在
img
标记中使用实际的 URI。""
对我有用。The above approaches don't seem to work anymore.
It seems that you now have to use an actual URI in the
img
tag.Things work for me with
"<img src=\"" + new File(...).toURI() + "\">"
.HTML 不支持嵌入图像。因此,您必须使用 setIcon 或向 JLabel 构造函数提供 ImageIcon; HTML 不能有 IMG 标签。
在您的情况下,您需要使用 JTextPane显示 HTML。请参阅教程此处
Embedded images are not supported in the HTML. Thus, you have to use setIcon or supply an ImageIcon to the JLabel constructor; the HTML cannot have an IMG tag.
In your case you need to use JTextPane to display HTML. See tutorial here