-2

それを実行すると、コンソールにこのエラーメッセージが表示されます。どこが間違っているのですか?「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);
        }
    }
}
4

4 に答える 4

5

あなたの最初の間違いはこの行です。

    System.err.println("Error displaying " + url);

間違いはそれがしていることではなく、それがしていないことにあります。また、(少なくとも)例外メッセージ、できればスタックトレースを出力する必要があります。


そして、スタックトレースを確認/表示したので、根本的なエラーが何であるかは明らかです。文字列"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()

于 2012-06-08T06:23:03.990 に答える
3

実際に何が起こっているのかを知るために、スタックトレースを表示する必要があります。ioe.printStackTrace()

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

public class ExempleText {

    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);
            ioe.printStackTrace();
        }
    }
}
于 2012-06-08T06:33:50.873 に答える
0

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();
}
于 2012-06-08T06:49:47.957 に答える
0

修正されたコード

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);
    }
  }
}
于 2012-06-08T06:43:10.363 に答える