2012-07-30 63 views
2

現在我希望acheieve的目標是讓HTML鏈接(每個都有一個專用於它們的選項卡)觸發setSelectedComponent JTabbedPane函數。換句話說,而不是跳到「全部」選項卡(這是頁面的html版本)的部分,我希望它切換選項卡。請注意,如果這是不可能的,是否有可能讓它們跳到像在瀏覽器中那樣的部分(因爲它本身不工作)?要觸發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分離爲頁面主體(varaible: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

預先感謝任何幫助! :)

+0

看一看http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JEditorPane.html,第二部分「關注超文本鏈接」 – MadProgrammer 2012-07-30 20:47:10

+0

未找到 請求的URL /〜hall/java/Swing的教程/ Swing的教程,JEditorPane.h在此服務器上找不到tml「。 – KisnardOnline 2012-07-30 23:17:17

+0

@MadProgrammer你有一個新的鏈接? – KisnardOnline 2012-07-30 23:17:32

回答

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