2009-08-07 2 views
1

나는 내 컴퓨터에서 선택한 HTML 파일을 보여주는이 코드를 쓰고 난은 faq.html처럼 내 컴퓨터에 HTML 파일을 선택할 때이 오류 메시지가 표시됩니다!이 코드가 HTML 파일을 표시하지 않는 이유는 무엇입니까?

java.net.MalformedURLException: no protocol: FAQ.html 
at java.net.URL.<init>(Unknown Source) 
at java.net.URL.<init>(Unknown Source) 
at java.net.URL.<init>(Unknown Source) 
at javax.swing.JEditorPane.setPage(Unknown Source) 
at org.bihe.com1112.FileViewer.actionPerformed(FileViewer.java:86) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 





public class FileViewer extends JPanel implements ActionListener { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

JFileChooser chooser; 

FileNameExtensionFilter filter = null; 

JTextField text; 

JButton button; 

FileInputStream in; 

JEditorPane pane; 

public FileViewer(JEditorPane pane) { 
    this.pane = pane; 
    setLayout(new FlowLayout(FlowLayout.RIGHT)); 
    text = new JTextField("file...", 31); 
    text.setColumns(45); 
    text.revalidate(); 
    text.setEditable(true); 

    button = new JButton("Browse"); 
    add(text); 
    add(button); 
    filter = new FileNameExtensionFilter("html", "html"); 
    chooser = new JFileChooser(); 
    chooser.addChoosableFileFilter(filter); 

    button.addActionListener(this); 

} 

public void paintComponent(Graphics g) { 
    super.paintComponents(g); 
    Graphics2D graphic = (Graphics2D) g; 
    graphic.drawString("HTML File:", 10, 20); 

} 

public void actionPerformed(ActionEvent event) { 
    int returnVal = 0; 
    if (event.getSource() == button) { 
     returnVal = chooser.showOpenDialog(FileViewer.this); 
     if (returnVal == JFileChooser.APPROVE_OPTION) { 
      File file = chooser.getSelectedFile(); 
      text.setText(file.getName()); 
      if (file != null) { 
       try { 
        pane.setPage(file.getName()); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } else 

       System.err.println("Couldn't find this HTML file:" 
         + file.getName()); 

     } else 
      System.exit(0); 
    } 

    } 
} 
+0

오류 메시지가 표시됩니까? –

답변

3

에게 당신을 같은 파일 프로토콜을 사용하여 파일의 전체 경로를 지정해야합니다

file:///c:/somefolder/FAQ.html 

당신은 URL 얻기 위해 다음 uri.toURL()를 URI에를 얻을 수 file.toURI()을 사용하고 있습니다 :

// file.toURL() has been deprecated, use file.toURI().toURL() instead 
pane.setPage(file.toURI().toURL()); 
을 0
+0

그러면 문자열이됩니다 !!! – Johanna

+0

그래서? URL을 문제없이 "JEditorPane.setPage()"에 문자열로 전달할 수 있습니다 : http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html#setPage%28java. lang.String % 29 –

+2

대답이 작동하지 않는다고 말하기 전에 시도하십시오. –

0

pane.setPage (file.getName()) 대신 pane.setPage (file.toURL())를 사용해보십시오. setPage는 url을 기대하기 때문에 작동하지 않게하려고하는 다른 사람들의 빠른 검색에서 판단 할 수 있습니다. 나 자신을 시험해 보았다.

관련 문제