JLabel可以有img标签吗

发布于 2024-08-26 03:25:19 字数 352 浏览 3 评论 0 原文

我正在尝试显示一个 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。

谢谢

I am trying to display a JLabel which has a few lines of text and an image as follows:

String html = "<html> hello </br> <img src = \"/absolute/path/here\" height = \"30\"  width =\"40\"/> </html>";
JLabel l = new JLabel(html);

For the image all I get is a broken image, is it possible to nest img tags inside a JLabel?

EDIT:
I want to add multiple images to the JLabel so I don't think the use of an ImageIcon will do here.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

乖乖公主 2024-09-02 03:25:19
File f = new File("C:\image.jpg"); 
jLabel1.setText("<html><img src=\"file:"+f.toString()+"\">");

这对我有用。它很简单,可以放置任意数量的图像,而不仅仅是一个图像图标。如果没有引号,它就不起作用。

File f = new File("C:\image.jpg"); 
jLabel1.setText("<html><img src=\"file:"+f.toString()+"\">");

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.

风渺 2024-09-02 03:25:19

对于图像,我得到的只是一个损坏的图像,是否可以将 img 标签嵌套在 JLabel 中

可以可以显示图像)在 JLabel 的文本中。您收到的图像已损坏,因为路径不正确。您需要在路径前添加 file: 前缀,或者最好让 java 使用 class.getResource("/your/path") 为您完成此操作。这是一个工作示例,只需插入有效的资源路径。

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel; 

public class MultipleImagesExample
{

  public static void main(String[] args)
  {

      JFrame frame = new JFrame();
      frame.setLayout(new BorderLayout());
      JLabel label = new JLabel(
          "<html>"
          + "<img src=\""
          + MultipleImagesExample.class.getResource("/resource/path/to/image1")
          + "\">"
          + "<img src=\""
          + MultipleImagesExample.class.getResource("/resource/path/to/image2")
          + "\">"
          + "The text</html>");
      frame.add(label, BorderLayout.CENTER);
      frame.setBounds(100, 100, 200, 100);
      frame.setVisible(true);
   }

 }

对于 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 with class.getResource("/your/path"). Here is a working example, just insert valid resource paths.

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel; 

public class MultipleImagesExample
{

  public static void main(String[] args)
  {

      JFrame frame = new JFrame();
      frame.setLayout(new BorderLayout());
      JLabel label = new JLabel(
          "<html>"
          + "<img src=\""
          + MultipleImagesExample.class.getResource("/resource/path/to/image1")
          + "\">"
          + "<img src=\""
          + MultipleImagesExample.class.getResource("/resource/path/to/image2")
          + "\">"
          + "The text</html>");
      frame.add(label, BorderLayout.CENTER);
      frame.setBounds(100, 100, 200, 100);
      frame.setVisible(true);
   }

 }

For more complex HTML in java, I recommend xhtmlrenderer.

热血少△年 2024-09-02 03:25:19

除非您对 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.

彻夜缠绵 2024-09-02 03:25:19

与其尝试在单个 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.

香橙ぽ 2024-09-02 03:25:19

使用 JEditorPane 显示 HTML。您可以更改背景、前景、字体等,使其看起来像一个标签。

Use a JEditorPane to display the HTML. You can change the background, forground, font etc so it looks like a label.

他不在意 2024-09-02 03:25:19

上面的方法似乎不再起作用了。

看来您现在必须在 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() + "\">".

够运 2024-09-02 03:25:19

HTML 不支持嵌入图像。因此,您必须使用 setIcon 或向 JLabel 构造函数提供 ImageIcon; HTML 不能有 IMG 标签。

  JLabel imageLabel =
  new JLabel(labelText,
             new ImageIcon("path/to/image.gif"),
             JLabel.CENTER);

在您的情况下,您需要使用 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.

  JLabel imageLabel =
  new JLabel(labelText,
             new ImageIcon("path/to/image.gif"),
             JLabel.CENTER);

In your case you need to use JTextPane to display HTML. See tutorial here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文