JEditorPane.scrollToReference(字符串)不工作

JEditorPane.scrollToReference(String) not working

所以我有一个正在编写的桌面应用程序,我想添加一些帮助文档。它使用 cardLayout,所以我创建了另一个 JPanel "card",其中包含一个包含 JEditorPane 的 JScrollPane。一切都显示正常,但我无法像在浏览器中打开 help.html 文件时那样使超链接(指向同一页面上的锚点的链接)正常工作。这是我的 SSCCE:

package com.example.test;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
import javax.swing.plaf.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

class myClass {
    public static void main(final String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        f.setTitle("JEditorPane Test");

        String html=null;
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/com/example/test/help.html")));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line = br.readLine()) != null)
                sb.append(line);
            html = sb.toString();
        }catch (Exception e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            System.out.println(sw.toString());
            html = "<html><font id=\"TOC\" size=\"+2\"><b>404 Error:</b></font><br/><br/>&emsp;&emsp;&emsp;<u>/com/swbc/datamasking/help.html</u> <b>not found.</b><br/><br/>&emsp;&emsp;&emsp;<font size=\"-1\"><i>Press ESC key to continue.</i></font></html>";
        }
        JEditorPane ep = new JEditorPane();
        ep.setContentType("text/html");
        ep.setText(text);
        ep.setEditable(false);

        JScrollPane sp = new JScrollPane(ep);
        f.add(sp);
        f.setBounds(200, 200, 400, 400);

        ep.addHyperlinkListener(new HyperlinkListener() {
        @Override public void hyperlinkUpdate(HyperlinkEvent pE) {
            if (HyperlinkEvent.EventType.ACTIVATED == pE.getEventType()) {
            String desc = pE.getDescription();
            if (desc == null || !desc.startsWith("#")) return;
            desc = desc.substring(1);
            ep.scrollToReference(desc);
            }
        }
        });

        f.setVisible(true);
    }
}

以下是它试图加载的 "help.html" 文件的编辑内容(标签和属性)未经编辑:

<html><body>
    <font style="word-wrap:break-word;font-weight:bold;">
        <a href="#Intro" id="TOP">Help File Intro</a><br/><br/>
        <a href="#Topic1">Topic #1</a><br/><br/>
        <a href="#Topic2">Topic #2</a><br/><br/>
        <a href="#Topic3">Topic #3</a><br/><br/>
        &emsp;&emsp;<a href="#Topic3a">Topic #3a</a><br/><br/>
        &emsp;&emsp;<a href="#Topic3b">Topic #3b</a><br/><br/>
        <a href="#Topic4">Topic #4</a><br/><br/>
        <a href="#Close">Closing</a><br/><br/>
    </font>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <HR width="80%" size="2" color="black">
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <a size="+1" id="Intro" style="word-wrap:break-word;font-weight:bold;" href="#TOP" title="Click here to return to questions">Help File Intro</a><br/><br/>
    <font style="word-wrap:break-word">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean a maximus elit. Donec tincidunt semper lectus vel scelerisque. Vivamus in sapien sit amet nisi maximus tincidunt nec et quam. Vivamus euismod posuere neque, at scelerisque mi iaculis ac. Quisque a enim id lorem sodales consequat. Vestibulum bibendum metus sagittis, suscipit arcu ac, faucibus mauris. Ut odio elit, sollicitudin in nisl tristique, semper suscipit purus. Duis mollis lectus non ex tincidunt, quis aliquam enim dapibus. Praesent tincidunt libero ac eros rutrum.</font>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <a size="+1" id="Topic1" style="word-wrap:break-word;font-weight:bold;" href="#TOP" title="Click here to return to questions">Topic #1</a><br/><br/>
    <font style="word-wrap:break-word">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean a maximus elit. Donec tincidunt semper lectus vel scelerisque. Vivamus in sapien sit amet nisi maximus tincidunt nec et quam. Vivamus euismod posuere neque, at scelerisque mi iaculis ac. Quisque a enim id lorem sodales consequat. Vestibulum bibendum metus sagittis, suscipit arcu ac, faucibus mauris. Ut odio elit, sollicitudin in nisl tristique, semper suscipit purus. Duis mollis lectus non ex tincidunt, quis aliquam enim dapibus. Praesent tincidunt libero ac eros rutrum.</font>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <a size="+1" id="Topic2" style="word-wrap:break-word;font-weight:bold;" href="#TOP" title="Click here to return to questions">Topic #2</a><br/><br/>
    <font style="word-wrap:break-word">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean a maximus elit. Donec tincidunt semper lectus vel scelerisque. Vivamus in sapien sit amet nisi maximus tincidunt nec et quam. Vivamus euismod posuere neque, at scelerisque mi iaculis ac. Quisque a enim id lorem sodales consequat. Vestibulum bibendum metus sagittis, suscipit arcu ac, faucibus mauris. Ut odio elit, sollicitudin in nisl tristique, semper suscipit purus. Duis mollis lectus non ex tincidunt, quis aliquam enim dapibus. Praesent tincidunt libero ac eros rutrum.</font>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <a size="+1" id="Topic3" style="word-wrap:break-word;font-weight:bold;" href="#TOP" title="Click here to return to questions">Topic #3</a><br/><br/>
    <font style="word-wrap:break-word">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean a maximus elit. Donec tincidunt semper lectus vel scelerisque. Vivamus in sapien sit amet nisi maximus tincidunt nec et quam. Vivamus euismod posuere neque, at scelerisque mi iaculis ac. Quisque a enim id lorem sodales consequat. Vestibulum bibendum metus sagittis, suscipit arcu ac, faucibus mauris. Ut odio elit, sollicitudin in nisl tristique, semper suscipit purus. Duis mollis lectus non ex tincidunt, quis aliquam enim dapibus. Praesent tincidunt libero ac eros rutrum.</font>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <a size="+1" id="Topic3a" style="word-wrap:break-word;font-weight:bold;" href="#TOP" title="Click here to return to questions">Topic #3a</a><br/><br/>
    <font style="word-wrap:break-word">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean a maximus elit. Donec tincidunt semper lectus vel scelerisque. Vivamus in sapien sit amet nisi maximus tincidunt nec et quam. Vivamus euismod posuere neque, at scelerisque mi iaculis ac. Quisque a enim id lorem sodales consequat. Vestibulum bibendum metus sagittis, suscipit arcu ac, faucibus mauris. Ut odio elit, sollicitudin in nisl tristique, semper suscipit purus. Duis mollis lectus non ex tincidunt, quis aliquam enim dapibus. Praesent tincidunt libero ac eros rutrum.</font>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <a size="+1" id="Topic3b" style="word-wrap:break-word;font-weight:bold;" href="#TOP" title="Click here to return to questions">Topic #3b</a><br/><br/>
    <font style="word-wrap:break-word">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean a maximus elit. Donec tincidunt semper lectus vel scelerisque. Vivamus in sapien sit amet nisi maximus tincidunt nec et quam. Vivamus euismod posuere neque, at scelerisque mi iaculis ac. Quisque a enim id lorem sodales consequat. Vestibulum bibendum metus sagittis, suscipit arcu ac, faucibus mauris. Ut odio elit, sollicitudin in nisl tristique, semper suscipit purus. Duis mollis lectus non ex tincidunt, quis aliquam enim dapibus. Praesent tincidunt libero ac eros rutrum.</font>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <a size="+1" id="Topic4" style="word-wrap:break-word;font-weight:bold;" href="#TOP" title="Click here to return to questions">Topic #4</a><br/><br/>
    <font style="word-wrap:break-word">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean a maximus elit. Donec tincidunt semper lectus vel scelerisque. Vivamus in sapien sit amet nisi maximus tincidunt nec et quam. Vivamus euismod posuere neque, at scelerisque mi iaculis ac. Quisque a enim id lorem sodales consequat. Vestibulum bibendum metus sagittis, suscipit arcu ac, faucibus mauris. Ut odio elit, sollicitudin in nisl tristique, semper suscipit purus. Duis mollis lectus non ex tincidunt, quis aliquam enim dapibus. Praesent tincidunt libero ac eros rutrum.</font>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <a size="+1" id="Close" style="word-wrap:break-word;font-weight:bold;" href="#TOP" title="Click here to return to questions">In Conclusion</a><br/><br/>
    <font style="word-wrap:break-word">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean a maximus elit. Donec tincidunt semper lectus vel scelerisque. Vivamus in sapien sit amet nisi maximus tincidunt nec et quam. Vivamus euismod posuere neque, at scelerisque mi iaculis ac. Quisque a enim id lorem sodales consequat. Vestibulum bibendum metus sagittis, suscipit arcu ac, faucibus mauris. Ut odio elit, sollicitudin in nisl tristique, semper suscipit purus. Duis mollis lectus non ex tincidunt, quis aliquam enim dapibus. Praesent tincidunt libero ac eros rutrum.</font>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <a size="+1" id="Tips" style="word-wrap:break-word;font-weight:bold;" href="#TOP" title="Click here to return to questions">Top Secret Hints & Tips (shhhh!)</a><br/><br/>
    <font style="word-wrap:break-word">Sorry no secrets here.<br/><br/><i>Check back later for some secret tips.</i></font><br/><br/><br/><br/>
</body></html>

是的,是的,我知道;它很难看,没有使用 CSS,等等。不是我写的,是一个不在身边的同事写的。我打算重写它,但我需要先证明这是一个可行的解决方案。如果不是,我会写一堆文本框。 :-/

事实证明,java 代码绝对没有问题。除此之外,JEditorPane HTMLEditorKit 中的默认解析器不如当今大多数浏览器灵活。必须修改 HTML 以将 id 属性更改为 name 属性,然后一切都按计费方式工作。

<a href="#Intro" id="TOP">

转换为:

<a href="#Intro" name="TOP">