【问题标题】:can JLabel have img tagsJLabel可以有img标签吗
【发布时间】:2018-06-05 18:05:14
【问题描述】:

我正在尝试显示一个包含几行文本和一个图像的 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 不会。

谢谢

【问题讨论】:

  • 没关系,但我相信你的意思是&lt;br /&gt;

标签: java html image jlabel


【解决方案1】:

For the image all I get is a broken image, is it possible to nest img tags inside a 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

【讨论】:

    【解决方案2】:
    File f = new File("C:\image.jpg"); 
    jLabel1.setText("<html><img src=\"file:"+f.toString()+"\">");
    

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

    【讨论】:

      【解决方案3】:

      除非您对 JEditorPane 感到满意,否则您基本上是在 Swing 中查看完整的网络浏览器。

      理想情况下,您可以使用 JWebPane,它是一个 WebKit 视图作为 Swing 组件,但它还没有推出。我能找到的最新信息是blog post

      The DJ project 允许在 Swing 中嵌入平台的本机浏览器。它在 Windows 上使用 Internet Explorer,在 Linux 上使用 XULRunner。它不支持 Mac。

      【讨论】:

        【解决方案4】:

        然后尝试在单个 JLabel 上拥有多个图像,为什么不简单地拥有多个 JLabel,每个 JLabel 都有一个图像(如 uthark 所述),然后将所有标签组合在一个 JPanel 上。这应该会以最小的额外复杂性为您提供您正在寻找的效果。

        【讨论】:

        • 您可以使用嵌入的 HTML 在 JLabel 上拥有多个 图像。您不能设置多个图标
        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

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

          看来您现在必须在 img 标记中使用实际的 URI。

          "&lt;img src=\"" + new File(...).toURI() + "\"&gt;" 对我有用。

          【讨论】:

          • 也适合我。 +1
          【解决方案7】:

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

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

          在您的情况下,您需要使用 JTextPane 来显示 HTML。见教程here

          【讨论】:

          • 如果我想在标签的不同行上有多个图像怎么办?
          猜你喜欢
          • 2011-02-07
          • 1970-01-01
          • 1970-01-01
          • 2010-09-28
          • 1970-01-01
          • 2014-05-31
          • 2015-12-22
          • 2010-09-22
          • 1970-01-01
          相关资源
          最近更新 更多