2017-07-02 151 views
0

我一直試圖打開htmlfile單擊幫助按鈕。我需要將文件導入到源代碼包中,以便在構建它之後不會顯示錯誤。我正在使用netbeans。當我將文件複製到src並嘗試運行該文件時,它在編譯時顯示錯誤。我想知道如何將文件添加到src以及如何通過向用戶提供系統中安裝的瀏覽器列表來打開文件。下面是我搜索的代碼和tried..Thanks提前如何打開一個html文件?

try 
    {   
     File htmlFile = new File(this.getClass().getResource("help.html").getFile()); 
     Desktop.getDesktop().browse(htmlFile.toURI()); 
    } 
    catch (IOException ex) 
    { 
     System.out.println(ex); 
    } 
+0

你必須給我們的例外。 –

+0

@KarthikeyanVaithilingam對不起,我沒有運行,因爲它顯示代碼編譯有錯誤。如果我將文件添加到'src'包中,代碼不會成功編譯 – Previn

+0

您必須給我們編譯錯誤 –

回答

0

我與我的桌面html文件嘗試。現在它的工作正常。 (我的默認瀏覽器是chrome

試試吧。

import java.awt.BorderLayout; 
import java.awt.Button; 
import java.awt.Desktop; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class FileOpenBrowser { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     JFrame frame = new JFrame("FileOpenBrowser"); // set API Name 
     JPanel topPanel = new JPanel(new FlowLayout()); // set Panel Layout 

     File urlDesktop = new File(
       "C://Documents and Settings/Hariharan/Desktop/help.html"); 

     Button btn = new Button("Help"); 
     btn.setBounds(50, 100, 60, 30); 
     topPanel.add(btn); 

     frame.add(topPanel, BorderLayout.PAGE_START); 

     frame.setSize(300, 300); 
     frame.setVisible(true); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     btn.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       try { 
        Desktop.getDesktop().open(urlDesktop); 
       } catch (Exception e1) { 
        e1.printStackTrace(); 
       } 
      } 
     }); 
    } 
} 
+0

但是在編譯之後它肯定會顯示錯誤,因爲你提到了文件的路徑..但​​是我需要將'.html文件'添加到'src'文件夾本身,然後調用它 – Previn

0

試試這個。

enter image description here

import java.awt.BorderLayout; 
import java.awt.Button; 
import java.awt.Desktop; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class FileOpenBrowser { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     JFrame frame = new JFrame("FileOpenBrowser"); // set API Name 
     JPanel topPanel = new JPanel(new FlowLayout()); // set Panel Layout 

     Button btn = new Button("Help"); 
     btn.setBounds(50, 100, 60, 30); 
     topPanel.add(btn); 

     frame.add(topPanel, BorderLayout.PAGE_START); 

     frame.setSize(300, 300); 
     frame.setVisible(true); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     btn.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       try { 
        System.out.println("Start.."); 
        File file = new java.io.File("src/help.html").getAbsoluteFile(); 
        Desktop.getDesktop().open(file);      
        System.out.println("End.."); 
       } catch (Exception e1) { 
        e1.printStackTrace(); 
       } 
      } 
     }); 
    } 
}