用于触发JTabbedPane切换选项卡的HTML

时间:2012-07-30 19:06:10

标签: java html swing jtabbedpane jeditorpane

现在我想要实现的目标是让HTML链接(每个链接都有一个专用于它们的选项卡)触发setSelectedComponent JTabbedPane函数。换句话说,而不是跳到“全部”选项卡上的部分(这是页面的html版本),我希望它切换标签。请注意,如果无法做到这一点,是否可以让它们像在浏览器中一样跳转到这些部分(因为这不是本地工作)?

<nav>
    [ <a href="gameplayhelp.php#Basic">Basic</a> | 
    <a href="gameplayhelp.php#Maps">Maps</a> | 
    <a href="gameplayhelp.php#Quests">Quests</a> | 
    <a href="gameplayhelp.php#NPCs">NPCs</a> | 
    <a href="gameplayhelp.php#Monsters">Monsters</a> | 
    <a href="gameplayhelp.php#Items">Items</a> | 
    <a href="gameplayhelp.php#Marketplace">Marketplace</a> | 
    <a href="gameplayhelp.php#Skills">Skills</a> | 
    <a href="gameplayhelp.php#Storage">Storage</a> ]
</nav>

enter image description here

以下是创建此图片的相关代码。上面的大部分代码解析我的网站,并将HTML分成只有页面正文(可变:htmlContent)和每个帮助部分(变量:helpSection)。

JScrollPane scrollPane = new JScrollPane();
JEditorPane editorPane = new JEditorPane();
scrollPane.setViewportView(editorPane);
editorPane.setEditorKit(kit);
editorPane.setEditable(false);
editorPane.setContentType("text/html");
editorPane.setText(htmlContent);
editorPane.setCaretPosition(0);
tabbedPane.addTab("All", null, scrollPane, "All gameplay help");

for(String s: navLinks){
    tabbedPane.addTab(s, null, new JScrollPane(new JEditorPane("text/html", helpSection.get(0))), s + " gameplay help");
    helpSection.remove(0);
}

如果有人想看一下我正在解析的html,那就是:

http://www.kisnardonline.com/gameplayhelp.php

提前感谢您对此的任何帮助! :)

2 个答案:

答案 0 :(得分:2)

好的,我只是看了一遍,看来评论中的那个被截断了:P

Following Hypertext Links

答案 1 :(得分:0)

这是我的最终解决方案:

public void hyperlinkUpdate(HyperlinkEvent event) {
    if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
        Pattern p = Pattern.compile(".*?(?:[a-z][a-z]+).*?(?:[a-z][a-z]+).*?((?:[a-z][a-z]+))",Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
        Matcher m = p.matcher(event.getDescription());
        if (m.find()){
            String word1=m.group(1);
            System.out.println(word1);
            if (navLinks.contains(word1)){
                tabbedPane.setSelectedIndex(navLinks.indexOf(word1)+1);
            }
        }
    }
  }