Java 我的程序有什么问题?我知道我犯了一个错误。但是在哪里呢?

Java 我的程序有什么问题?我知道我犯了一个错误。但是在哪里呢?,java,swing,Java,Swing,运行它时,我在控制台中收到此错误消息。我哪里出错了? “显示C:/Users/Nimit/Desktop/n.txt时出错” 你的第一个错误是这句话 System.err.println("Error displaying " + url); 错误不在于它在做什么,而在于它没有做什么。还应该打印(至少)异常消息,最好是堆栈跟踪 现在,您已经看到/向我们展示了堆栈跟踪,很清楚潜在的错误是什么。字符串“C:/Users/Nimit/Desktop/n.txt”不是有效的URL。有效的文件

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


你的第一个错误是这句话

    System.err.println("Error displaying " + url);
错误不在于它在做什么,而在于它没有做什么。还应该打印(至少)异常消息,最好是堆栈跟踪


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

带有Windows驱动器号的文件URL的编写方式类似于
“file:///C:/Users/Nimit/Desktop/n.txt“

参考


或者,您可以使用
新文件(“某些路径”)将路径名转换为URL。toURL()

您应该使用
ioe.printStackTrace()

修正码

import java.io.*;
import javax.swing.*;
import java.net.*;

public class exmpleText
{

  public static void main(String[] args)
  {
    String url = "C:\\Users\\welcome\\z.txt";
    File f = new File(url);

    try
    {
      URL x = f.toURL();
      System.out.println(x);         

      JFrame frame=new JFrame("Hi");

      JEditorPane Pane = new JEditorPane(x);
      Pane.setEditable(false);

      frame.add(new JScrollPane(Pane));
      frame.setVisible(true);
    } 
    catch(Exception ioe) 
    {
      System.out.println(ioe);
      System.err.println("Error displaying " + url);
    }
  }
}

在JFrame对象创建之后添加此代码

 try{

editorPane.setContentType("text/html");

Reader fileRead=new FileReader(name); // name is the file you want to read

editorPane.read(fileRead,null);

} catch(Exception e) { 

e.printStackTrace();
}

导入org.jfree.ui.*;导入org.jfree.chart.*;导入org.jfree.chart.plot.*@约翰·桑德斯编辑的版本也不起作用。我收到了同样的错误信息,那行怎么了?控制台中正在打印错误消息。我收到消息“错误显示C:/Users/Nimit/Desktop/n.txt”。您正在打印URL,这无助于获取错误。请编写ioe.printStackTrace()以获取错误。我们想知道如何回答您的问题java.net.MalformedURLException:未知协议:java.net.URL处的c。(未知源)java.net.URL处的c。(未知源)位于javax.swing.JEditorPane.setPage(未知源代码)javax.swing.JEditorPane.(未知源代码)位于example.main(example.java:23)显示C://Users//Nimit//Desktop//n.txt时出错谢谢。它起作用了。我想知道控制流及其工作原理。将变量名声明为“url”,它将不会成为url。您需要一个文件对象来打开一个文件,然后处理它。
import java.io.*;
import javax.swing.*;
import java.net.*;

public class exmpleText
{

  public static void main(String[] args)
  {
    String url = "C:\\Users\\welcome\\z.txt";
    File f = new File(url);

    try
    {
      URL x = f.toURL();
      System.out.println(x);         

      JFrame frame=new JFrame("Hi");

      JEditorPane Pane = new JEditorPane(x);
      Pane.setEditable(false);

      frame.add(new JScrollPane(Pane));
      frame.setVisible(true);
    } 
    catch(Exception ioe) 
    {
      System.out.println(ioe);
      System.err.println("Error displaying " + url);
    }
  }
}
 try{

editorPane.setContentType("text/html");

Reader fileRead=new FileReader(name); // name is the file you want to read

editorPane.read(fileRead,null);

} catch(Exception e) { 

e.printStackTrace();
}