2012-07-01 56 views
1

我正在完成一個項目,並且我有一個簡單的html,其中包含一些圖像和錨鏈接,我想在用戶單擊幫助菜單按鈕時將其顯示爲幫助文件。 我不知道該怎麼做。我嘗試了下面的代碼,它在沒有任何滾動的框架中返回我唯一的並且鏈接不工作。在jframe中加載html文件

pane = new JEditorPane(getClass().getResource("help.html"));//add the html to be shown 
    pane.setEditable(false); 

有沒有簡單的方法呢? 請保持簡單,因爲我是Java新手。 謝謝!

回答

2
+0

謝謝!讀者和目標是什麼? – Vagelism

+0

[aaacch](http://www.java2s.com/Code/JavaAPI/javax.swing/JTextAreareadReaderinObjectdesc.htm),爲所有[JTextComponents](http:// docs。)實現了'read' /'write'方法。 oracle.com/javase/tutorial/uiswing/components/text.html) – mKorbel

2

好,因爲我不能設法做到這一點在其他的方式,我發現更多有用的使用默認用戶瀏覽器查看文件。 所以這是我的解決方案:

helpItem.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      Desktop desktop = null; 
      // Before more Desktop API is used, first check 
      // whether the API is supported by this particular 
      // virtual machine (VM) on this particular host. 
      if (Desktop.isDesktopSupported()) 
      { 
      try 
      { 
       desktop = Desktop.getDesktop(); 
       String thehelpfile= String.valueOf(getClass() 
         .getResource("help.html")); 
       File fileToOpen=new File(thehelpfile.substring(5)); 
       desktop.open(fileToOpen); 
      } catch (IOException ex) 
      { 
       JOptionPane.showMessageDialog(null, 
         ex, "Λάθος", JOptionPane.PLAIN_MESSAGE); 
      } 
      } 
     } 
    }); 
+1

+1 for self_answering – mKorbel