Java组件的HTML应该如何引用资源?

 手机用户2502873667 发布于 2022-12-12 17:25

在以下情况下,可能无法正确加载HTML(样式表,图像等)的资源:

一个Swing应用程序.是分布式的,或者当它被放入Jar文件中时.

HTML是在运行时生成的.

如何在HTML中可靠地访问这些资源?

1 个回答
  • 来自Jar文件的HTML通过相对引用链接资源(例如CSS或图像)将正常工作.

    例如

    此示例从Jar加载HTML(具有对图像的相对引用).

    import javax.swing.*;
    import java.net.URL;
    
    class ShowHtml {
    
        public static void main(String[] args) {
            final String address =
                "jar:http://pscode.org/jh/hs/object.jar!/popup_contents.html";
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    try {
                        URL url = new URL(address);
                        JEditorPane jep = new JEditorPane(url);
                        JFrame f = new JFrame("Show HTML in Jar");
                        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        f.add(new JScrollPane(jep));
                        f.pack();
                        f.setSize(400,300);
                        f.setLocationByPlatform(true);
                        f.setVisible(true);
                    } catch(Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    

    截图

    JEdi​​torPane显示Jar'd HTML

    HTML

    正在加载的HTML.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
    <!--       
     *        Copyright (C) 1997  Sun Microsystems, Inc
     *                    All rights reserved.
     *          Notice of copyright on this source code 
     *          product does not indicate publication. 
     * 
     * RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by 
     * the U.S. Government is subject to restrictions as set forth 
     * in subparagraph (c)(1)(ii) of the Rights in Technical Data
     * and Computer Software Clause at DFARS 252.227-7013 (Oct. 1988) 
     * and FAR 52.227-19 (c) (June 1987).
     *
     *    Sun Microsystems, Inc., 2550 Garcia Avenue,
     *    Mountain View, California 94043.
     *
    -->
    <HTML>
    <HEAD>
    <TITLE>
    Editing Project Attributes
    </TITLE>
    </HEAD>
    <BODY BGCOLOR="#ffffff">
    <IMG SRC="images/popup_icon.gif"  > <b>Popup Window</b>
    <p>
    Popup windows appear near the location from which they are
    activated.  They are not contained in frames and thus
    cannot be resized or moved by the user.  Popups are
    dismissed by clicking anywhere in the help viewer.
    <p>
    Popup windows can be activated by clicking on a text object, 
    graphic object, or JComponent button.  All three examples are
    included in this demo.
    <p>
    <A HREF="popup_contents2.html">More...</A>
    </body>
    </html>
    

    EG 2

    对于动态创建的HTML,JRE 可能会使用类文件的位置作为HTML的假定位置.但是为了消除所有疑问,我们可以在中指定base元素head.

    import javax.swing.*;
    
    class HtmlUsingBase {
    
        public static void main(String[] args) {
            final String htmlContent =
                "<html>" +
                "<head>" +
                "<base href='http://www.gravatar.com/'>" +
                "</head>" +
                "<body>" +
                "<h1>Image path from BASE</h1>" +
                "<img src='avatar/a1ab0af4997654345d7a9" +
                "49877f8037e?s=128&d=identicon&r=PG'" +
                "  >" +
                "</body>" +
                "</html>";
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JLabel label = new JLabel(htmlContent);
                    JOptionPane.showMessageDialog(null, label);
                }
            });
        }
    }
    

    截图

    在此输入图像描述

    2022-12-12 17:28 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有