JLabel HTML 中的相对路径

发布于 2024-08-08 12:16:28 字数 948 浏览 4 评论 0原文

我试图让 JLabel 显示一个使用相对路径引用图像的 html。 但我无法让 JLabel 找到图像。当我使用绝对路径时它工作得很好。

我尝试从命令行或从 Eclipse 运行该程序,并添加对话框以显示当前工作目录在哪里,但没有用。 因此,我得出的结论是,不会在当前目录中搜索该图像 - 这让我明白了这一点。在哪里寻找图像?

这是一个测试代码,显示我在做什么:

import javax.swing.*;

public class HTMLLabel extends JFrame {
    public HTMLLabel() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JOptionPane.showMessageDialog( this, 
                System.getProperty("user.dir"));

         String html = "<html>\n" +
        "   <body>\n" +
        "       <div style=\"text-align: center;\">\n" + 
        "           <img src=\"file://s.png\">\n"+
        "       </div>\n"+
        "   </body>\n"+
        "</html>";

         JLabel label = new JLabel(html);
         add(label);

         pack();
         setVisible(true);
    } 

    public static void main(String[] args) {
         new HTMLLabel();
    }
}

I am trying to make JLabel show an html which is referencing an image using a relative path.
But I cannot make JLabel locate the image. It works fine when I am using absolute path.

I have tried running the program from the command line or from eclipse and add dialog to show me where is the current working directory but for avail.
I have therefor came to the conclusion that the image is not searched in the current directory - which brings me to the point. where is the image looked for?

here is a test code that show what I am doing:

import javax.swing.*;

public class HTMLLabel extends JFrame {
    public HTMLLabel() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JOptionPane.showMessageDialog( this, 
                System.getProperty("user.dir"));

         String html = "<html>\n" +
        "   <body>\n" +
        "       <div style=\"text-align: center;\">\n" + 
        "           <img src=\"file://s.png\">\n"+
        "       </div>\n"+
        "   </body>\n"+
        "</html>";

         JLabel label = new JLabel(html);
         add(label);

         pack();
         setVisible(true);
    } 

    public static void main(String[] args) {
         new HTMLLabel();
    }
}

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

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

发布评论

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

评论(4

栖竹 2024-08-15 12:16:28

使用此函数为 JLabel 准备 html 文本,以显示与类包相关的图像。

public static String prepareHtmlToJLabelText(Class relativeClass, String html) {
    Pattern p = Pattern.compile("src=['\"](.*?)['\"]");
    Matcher m = p.matcher(html);
    while (m.find()) {
        html = html.replace(m.group(), "src='" + relativeClass.getResource(m.group(1)) + "'");
    }
    return html;
}

该函数替换“src”属性的内容,使其与提供程序类相关。

示例:

jLabel.setText(prepareHtmlToJLabelText(this.getClass(), "<html><div style='text-align: center;'><img src='imageA.png'></div>Bla bla bla...<div style='text-align: center;'><img src='imageB.png'></div>"));

无论如何,要获得真正的 html 支持,请使用 JEditorPane。

Use this function to prepare the html text for the JLabel to display images relative to the package of a class.

public static String prepareHtmlToJLabelText(Class relativeClass, String html) {
    Pattern p = Pattern.compile("src=['\"](.*?)['\"]");
    Matcher m = p.matcher(html);
    while (m.find()) {
        html = html.replace(m.group(), "src='" + relativeClass.getResource(m.group(1)) + "'");
    }
    return html;
}

The function replace the content of the "src" attribute to make it relative to the provider class.

Example:

jLabel.setText(prepareHtmlToJLabelText(this.getClass(), "<html><div style='text-align: center;'><img src='imageA.png'></div>Bla bla bla...<div style='text-align: center;'><img src='imageB.png'></div>"));

Anyway for a real html support use JEditorPane.

坐在坟头思考人生 2024-08-15 12:16:28

我看到两种变体:

我不知道为什么,但对我来说,

"                       <img src=\"file:s.png\">\n"+

假设 s.png 位于当前工作目录中,这是可行的。

另一种似乎更适合我的变体是:

URL url = HTMLLabel.class.getResource( "/s.png" );
  String html = "<html>\n" +
    "       <body>\n" +
    "               <div style=\"text-align: center;\">\n" + 
    "                       <img src=\""+url+"\">\n"+
    "               </div>\n"+
    "       </body>\n"+
    "</html>";

I see two variants:

I don't know why but for me this works

"                       <img src=\"file:s.png\">\n"+

assuming that s.png is in current working directory.

Another variant that seems more appropriate for me is:

URL url = HTMLLabel.class.getResource( "/s.png" );
  String html = "<html>\n" +
    "       <body>\n" +
    "               <div style=\"text-align: center;\">\n" + 
    "                       <img src=\""+url+"\">\n"+
    "               </div>\n"+
    "       </body>\n"+
    "</html>";
笨笨の傻瓜 2024-08-15 12:16:28

你为什么要这样做?只需使用这个 JLabel(Icon image) 构造函数

JLabel label = new JLabel(createImageIcon("s.png","description"));

protected ImageIcon createImageIcon(String path, String description) {
  java.net.URL imgURL = getClass().getResource(path);
  if (imgURL != null) {
    return new ImageIcon(imgURL, description);
  } else {
    System.err.println("Couldn't find file: " + path);
    return null;
  }
}

或者如果您坚持使用 html 变体。

顺便说一句,文件协议使用 3 个斜杠(file://s.png 无效),file:///s.png 表示 C:\s .png。如果图像位于您可以使用的起始目录中。

String path = System.getProperty("user.dir");
String html =
  "<html>\n" +
     "<body>\n" +
        "<div style=\"text-align: center;\">\n" +
          "<img src=\"file:///"+path+"/s.png\">\n"+
        "</div>\n"+
     "</body>\n"+
  "</html>";

但我对第二个解决方案不做任何保证。

Why are you even doing it like this? Just use this JLabel(Icon image) constructor

JLabel label = new JLabel(createImageIcon("s.png","description"));

protected ImageIcon createImageIcon(String path, String description) {
  java.net.URL imgURL = getClass().getResource(path);
  if (imgURL != null) {
    return new ImageIcon(imgURL, description);
  } else {
    System.err.println("Couldn't find file: " + path);
    return null;
  }
}

Or if you insist on the html variant.

btw The file protocol uses 3 slashes (file://s.png is invalid) and file:///s.png would mean C:\s.png. If the image resides in the starting directory you could use.

String path = System.getProperty("user.dir");
String html =
  "<html>\n" +
     "<body>\n" +
        "<div style=\"text-align: center;\">\n" +
          "<img src=\"file:///"+path+"/s.png\">\n"+
        "</div>\n"+
     "</body>\n"+
  "</html>";

But I make no guarantees about the 2nd solution.

↘人皮目录ツ 2024-08-15 12:16:28

我刚刚替换了你的

<img src=\"file://s.png\">\n"
//with
<img src=\"file:///s.png\">\n"+

,解决了问题

注意:我将 s.png 文件放在 src/java 文件夹中

i just replaced your

<img src=\"file://s.png\">\n"
//with
<img src=\"file:///s.png\">\n"+

and that solved the problem

NOTE: i placed the s.png file in the src/java folder

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