java - HTML for Java 组件应该如何引用资源?

标签 java html swing jlabel jeditorpane

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();
                }
            }
        });
    }
}

截图

JEditorPane displaying 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" 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>

例如2

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

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);
            }
        });
    }
}

截图

enter image description here

关于java - HTML for Java 组件应该如何引用资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26795598/

相关文章:

javascript - html span 类导致链接错误地居中

java - 当红色矩形与青色矩形碰撞时如何将其移除?

java - 我有一个打开 GUI 的按钮,如何设置 GUI 在屏幕上显示的位置?

java - 让 XText 发挥作用

java - 为什么我们在标记清除垃圾收集期间需要 "sweep"?

html - 在 Angular 中,如何在选择时将 <option> 文本设为粗体?

javascript - 使用 jQuery 选择除一个按钮之外的所有按钮

java - 同时使用 Neo4j 和 Ontotext GraphDB 的 GraphQL

java - build-wsdl2java.xml 是如何生成的?

java - JComboBox 是否有最大字段数?