【问题标题】:Why this code doesn't show the HTML file?为什么这段代码不显示 HTML 文件?
【发布时间】:2009-08-07 12:22:56
【问题描述】:

我编写了这段代码来显示我从计算机中选择的 HTML 文件!当我在计算机中选择 HTML 文件(如 FAQ.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);
    }

  }
 }

【问题讨论】:

  • 您是否收到错误消息?

标签: java html


【解决方案1】:

您需要使用文件协议指定文件的完整路径,如下所示:

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

您可以使用file.toURI() 获取URI,然后使用uri.toURL() 获取URL:

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

【讨论】:

  • 所以?您可以毫无问题地将 URL 作为字符串传递给 JEditorPane.setPage()java.sun.com/javase/6/docs/api/javax/swing/…
  • 我试过了。但它会显示一个编译错误,即类型字符串的 toURI 方法无法识别。字符串文件路径 = file.getAbsolutePath(); pane.setPage(filePath.toURI().toURL());
  • 哇,我弄错了:"> File filePath = file.getAbsoluteFile(); pane.setPage(filePath.toURI().toURL()); 谢谢一百万
【解决方案2】:

也许尝试 pane.setPage(file.toURL()) 而不是 pane.setPage(file.getName()),因为 setPage 需要一个 url,从快速搜索其他试图让它工作的人来看,没有自己试过了。

【讨论】:

    猜你喜欢
    • 2017-11-25
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    相关资源
    最近更新 更多