Java 为什么此代码没有';不显示HTML文件吗?

Java 为什么此代码没有';不显示HTML文件吗?,java,html,Java,Html,我写这段代码是为了显示HTML文件,我从我的计算机上选择了它!当我在计算机中选择类似FAQ.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

我写这段代码是为了显示HTML文件,我从我的计算机上选择了它!当我在计算机中选择类似FAQ.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);
    }

  }
 }
java.net.MalformedURLException:无协议:FAQ.html
位于java.net.URL。(未知源)
位于java.net.URL。(未知源)
位于java.net.URL。(未知源)
位于javax.swing.JEditorPane.setPage(未知源)
访问org.bihe.com1112.FileViewer.actionPerformed(FileViewer.java:86)
在javax.swing.AbstractButton.fireActionPerformed(未知源)
位于javax.swing.AbstractButton$Handler.actionPerformed(未知源)
在javax.swing.DefaultButtonModel.fireActionPerformed(未知源)
位于javax.swing.DefaultButtonModel.setPressed(未知源)
位于javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知源代码)
位于java.awt.Component.ProcessMouseeEvent(未知源)
位于javax.swing.JComponent.ProcessMouseeEvent(未知源)
位于java.awt.Component.processEvent(未知源)
位于java.awt.Container.processEvent(未知源)
位于java.awt.Component.dispatchEventImpl(未知源)
位于java.awt.Container.dispatchEventImpl(未知源)
位于java.awt.Component.dispatchEvent(未知源)
位于java.awt.LightweightDispatcher.RetargetMouseeEvent(未知源)
位于java.awt.LightweightDispatcher.ProcessMouseeEvent(未知源)
位于java.awt.LightweightDispatcher.dispatchEvent(未知源)
位于java.awt.Container.dispatchEventImpl(未知源)
位于java.awt.Window.dispatchEventImpl(未知源)
位于java.awt.Component.dispatchEvent(未知源)
位于java.awt.EventQueue.dispatchEvent(未知源)
位于java.awt.EventDispatchThread.pumpOneEventForFilters(未知源)
位于java.awt.EventDispatchThread.pumpEventsForFilter(未知源)
位于java.awt.EventDispatchThread.pumpEventsForHierarchy(未知源)
位于java.awt.EventDispatchThread.pumpEvents(未知源)
位于java.awt.EventDispatchThread.pumpEvents(未知源)
位于java.awt.EventDispatchThread.run(未知源)
公共类FileViewer扩展JPanel实现ActionListener{
/**
* 
*/
私有静态最终长serialVersionUID=1L;
JFileChooser选择器;
FileNameExtensionFilter过滤器=空;
JTextField文本;
按钮;
文件输入流输入;
窗格玻璃;
公共文件查看器(JEditorPane窗格){
this.pane=窗格;
setLayout(新的FlowLayout(FlowLayout.RIGHT));
text=新的JTextField(“文件…”,31);
text.setColumns(45);
text.revalidate();
text.setEditable(true);
按钮=新按钮(“浏览”);
添加(文本);
添加(按钮);
过滤器=新文件名扩展过滤器(“html”、“html”);
chooser=newjfilechooser();
chooser.addChoosableFileFilter(过滤器);
addActionListener(这个);
}
公共组件(图形g){
超级组件(g);
Graphics2D graphic=(Graphics2D)g;
graphic.drawString(“HTML文件:”,10,20);
}
已执行的公共无效操作(操作事件){
int returnVal=0;
if(event.getSource()==按钮){
returnVal=chooser.showOpenDialog(FileViewer.this);
if(returnVal==JFileChooser.APPROVE_选项){
File File=chooser.getSelectedFile();
text.setText(file.getName());
如果(文件!=null){
试一试{
setPage(file.getName());
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}否则
System.err.println(“找不到此HTML文件:”
+getName());
}否则
系统出口(0);
}
}
}

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

file:///c:/somefolder/FAQ.html
您可以使用获取URI,然后获取URL:

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

也许可以尝试pane.setPage(file.toURL())而不是pane.setPage(file.getName()),因为setPage需要一个url,这从快速搜索其他试图让它工作的人,而不是我自己来判断。

是否收到错误消息?那么呢?您可以将URL作为字符串传递给
JEditorPane.setPage()
,没有任何问题:我已经尝试过了。但它将显示一个编译错误,该错误导致类型字符串的toURI方法无法识别。字符串filePath=file.getAbsolutePath();setPage(filePath.toURI().toul());哇,我弄错了:“>File filePath=File.getAbsoluteFile();pane.setPage(filePath.toURI().toURL());非常感谢