One*_*ror 0 java user-interface swing input
我试图使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
要将参数作为URLfor JEditorPane.setPage,您可以使用:
File file = new File(filename);
editorpane.setPage(file.toURI().toURL());
Run Code Online (Sandbox Code Playgroud)
另外,不要忘记将您添加JEditorPane到框架中,以便可以看到:
add(editorScrollPane);
Run Code Online (Sandbox Code Playgroud)
要查看您要添加的磁盘错误:
e.printStackTrace();
Run Code Online (Sandbox Code Playgroud)
到IOException街区.
| 归档时间: |
|
| 查看次数: |
4553 次 |
| 最近记录: |