我的计划有什么问题?我知道我犯了一个错误.但是哪里?

Use*_*234 -2 java swing

运行时,我在控制台中收到此错误消息.我哪里出错了?"显示C:/Users/Nimit/Desktop/n.txt时出错"

import java.io.IOException;
import javax.swing.*;
public class exmpleText
{
    public static void main(String[] args)
    {
        String url = "C:/Users/Nimit/Desktop/n.txt";
        try
        {
            JFrame frame=new JFrame("Hi");
            JEditorPane Pane = new JEditorPane(url);
            Pane.setEditable(false);
            frame.add(new JScrollPane(Pane));
        } 
        catch(IOException ioe) 
        {
            System.err.println("Error displaying " + url);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*n C 5

你的第一个错误是这一行.

    System.err.println("Error displaying " + url);
Run Code Online (Sandbox Code Playgroud)

错误不在于它正在做什么,而是在做什么.还应该做的是打印(至少)异常消息,最好是堆栈跟踪.


现在您已经看到/向我们展示了堆栈跟踪,很清楚底层错误是什么.该字符串"C:/Users/Nimit/Desktop/n.txt"不是有效的URL.有效的文件URL以"file:"方案开头.

带有Windows驱动器号的文件URL写成"file:///C:/Users/Nimit/Desktop/n.txt".

参考:http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx

或者,您可以使用将路径名转换为URL new File("some path").toURL().