【问题标题】:Using JEditorPane to open a text file使用 JEditorPane 打开一个文本文件
【发布时间】:2012-09-18 22:42:12
【问题描述】:

我正在尝试使用 JEditorPane(在不可编辑模式下)打开框架中的文本文件。但是,我相信我在设置输入流和输出流时遇到了问题。请查看我的代码并告诉我哪里做错了。


import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class TextEditor extends JFrame{

private JEditorPane editorpane;
JScrollPane editorScrollPane;
String filename="D:\\abc.txt";
Reader filereader;

public TextEditor()
{       
        editorpane= new JEditorPane();
        editorpane.setEditable(false);

        if (filename != null) 
        {
            try 
            {
                filereader=new FileReader(filename);
                editorpane.setPage(filename);
            }

            catch (IOException e) 
            {
                System.err.println("Attempted to read a bad file " + filename);
            }
         }

        else
        {
            System.err.println("Couldn't find file");
        }

        //Put the editor pane in a scroll pane.
        editorScrollPane = new JScrollPane(editorpane);
            editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        editorScrollPane.setPreferredSize(new Dimension(250, 145));
        editorScrollPane.setMinimumSize(new Dimension(10, 10));

}

public static void main(String[] args) 
{
    TextEditor obj= new TextEditor();
    obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    obj.setSize(600,600);
    obj.setLocation(100,100);
    obj.setVisible(true);
}
}

【问题讨论】:

  • 您应该打印 IOException 的消息。这将为您提供有关正在发生的事情的真实提示:访问被拒绝?文件未找到?磁盘错误?
  • 感谢您的提示。现在错误消息是:java.io.FileNotFoundException: file:\D:\abc.txt(文件名、目录名或卷标语法不正确)
  • 试试“file:///D:/abc.txt”

标签: java swing user-interface input


【解决方案1】:

要将参数作为URL 用于JEditorPane.setPage,您可以使用:

File file = new File(filename);
editorpane.setPage(file.toURI().toURL());

另外不要忘记将您的JEditorPane 添加到框架中以便可以看到:

add(editorScrollPane);

要查看您收到的磁盘错误,请添加:

e.printStackTrace();

IOException 块。

【讨论】:

  • 还是同样的错误:试图读取一个错误的文件 file:\D:\abc.txt
  • 是的,文件存在。我可以通过在命令提示符下执行此操作来打开文件:D:\>abc.txt
  • 现在经过您的大力帮助,错误消息消失了。但我的框架仍然没有打开文件。只是出现一个空白框。
  • 是的。知道了。非常感谢先生!!
【解决方案2】:
editorpane.getEditorKit().read(filereader, editorpane.getDocument(), 0);

【讨论】:

  • 还是同样的错误:试图读取一个错误的文件 file:\D:\abc.txt
【解决方案3】:

JEditorPane.setPage() 需要一个 URL,包括协议:尝试“file:///D:/abc.txt”。以下代码应该可以工作:

String filename="file:///D:/abc.txt";

public TextEditor()
{
        editorpane= new JEditorPane();
        editorpane.setEditable(false);

        if (filename != null)
        {
            try
            {
                editorpane.setPage(filename);
            }

            catch (IOException e)
            {
                System.err.println("Attempted to read a bad file " + filename );
                e.printStackTrace();
            }
         }

        else
        {
            System.err.println("Couldn't find file");
        }

        //Put the editor pane in a scroll pane.
        editorScrollPane = new JScrollPane(editorpane);
            editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        editorScrollPane.setPreferredSize(new Dimension(250, 145));
        editorScrollPane.setMinimumSize(new Dimension(10, 10));
      getContentPane().add(editorScrollPane);

}

【讨论】:

  • 即使按照你说的做,还是有同样的错误信息:Attempted to read a bad file file:\D:\abc.txt
  • 您还需要删除 FileReader() 的创建——无论如何您都不使用它,它会抛出 IOError,因为它需要一个纯文件名,而不是 URL
  • 最后还需要在TextEditor构造函数的最后加上getContentPane().add(editorScrollPane);,不然什么都看不到……
猜你喜欢
  • 2015-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 2018-12-20
  • 2011-12-20
  • 2012-02-25
相关资源
最近更新 更多