当前位置: 首页 > 面试题库 >

HTML for Java组件应如何参考资源?

利博远
2023-03-14
问题内容

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

  • Swing应用程序。分发,或将其放入Jar文件时。
  • HTML在运行时生成。

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


问题答案:

通过相对
引用链接资源(例如CSS或图像)的Jar文件中的HTML即可正常工作。

例如
本示例从
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();
                }
            }
        });
    }
}

Screenshot

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" width="24" height="24"> <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>

E.G. 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'" +
            " width='128' height='128'>" +
            "</body>" +
            "</html>";
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JLabel label = new JLabel(htmlContent);
                JOptionPane.showMessageDialog(null, label);
            }
        });
    }
}


 类似资料:
  • http://docs.spring.io/spring-security/site/docs/4.2.2.RELEASE/reference/htmlsingle/ https://msdn.microsoft.com/en-us/library/ie/gg622941(v=vs.85).aspx https://en.wikipedia.org/wiki/Clickjacking https:

  • 这里有一些和风格指南有关的苹果的文档: The Objective-C Programming Language Cocoa Fundamentals Guide Coding Guidelines for Cocoa iOS App Programming Guide Apple Objective-C conventions: 来自苹果的代码约定 其他: Objective-Clean: an

  • 参考资料 Advanced Bash-Scripting Guide,非常详细,非常易读,大量example,既可以当入门教材,也可以当做工具书查阅 Unix Shell Programming Linux Shell Scripting Tutorial - A Beginner’s handbook

  • 这本书的内容基本上是我学习Go过程以及以前从事Web开发过程中的一些经验总结,里面部分内容参考了很多站点的内容,感谢这些站点的内容让我能够总结出来这本书,参考资料如下: golang blog Russ Cox blog go book golangtutorials 轩脉刃de刀光剑影 Go 官网文档 Network programming with Go setup-the-rails-app

  • seajs 实例解析 SeaJS 内部执行过程 - 从 use 说起 SeaJS v1.2 中文注释版 hello seajs seajs.org/docs 使用SeaJS实现模块化JavaScript开发 use.js harmony:modules harmony:module_loaders AMD规范 CMD规范 AMD 和 CMD 的区别有哪些? 与 RequireJS 的异同 基于Co

  • 光源组件定义了光源的类型,颜色,强度以及产生的阴影类型,在场景中加入光源能使模型渲染得更加立体。具体可参考 光照 章节。 点击 属性检查器 下面的 添加组件 按钮,然后从 渲染组件 中选择 Light,即可添加 Light 组件到节点上。 Light 属性 属性 功能说明 Type 支持的光源类型,包括 DIRECTIONAL(平行光)、POINT(点光源)、SPOT(聚光灯)和 AMBIENT