JEdi​​torPane.scrollToReference(String)不起作用

时间:2015-11-10 19:16:00

标签: java html swing jeditorpane

所以我有一个我正在编写的桌面应用程序,我想添加一些帮助文档。它使用了一个cardLayout,所以我创建了另一个JPanel“卡片”,其中包含一个包含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,等等,等等等等。我没有写它,一个不再写的同事。我打算重写它,但我需要在我做之前证明这是一个可行的解决方案。如果不是,我会写一堆文本框。 : - /

1 个答案:

答案 0 :(得分:0)

事实证明,java代码绝对没有错。除此之外,JEditorPane HTMLEditorKit中的默认解析器不像今天的大多数浏览器那样灵活。必须修改HTML以将id属性更改为name属性,然后所有内容都按照计费工作。

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

转换为:

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