以下程序是一个简单文本处理器,菜单项可以打开、编辑、保存一个文件。文件内容显示在下面的文本区域中(提示,打开文件通过文件选择器来完成)。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。运行结果如下图所示。
注意:不改动程序的结构,不得增行或删行。
import java.awt.*;
import java.awt.event.*;
import java.io.* ;
import javax.swing.*;
class FileFrame. extends JFrame
{
File file;
JTextPane textpane;
FileInputStream readStream;
JScrollPane scroll;
public FileFrame()
{
super ("文件浏览");
JMenu fileM = new JMenu("文件");
OpenAction pen = new OpenAction ();
SaveAction clear = new SaveAction ();
ExitAction exit = new ExitAction();
JMenuBar mb = new JMenuBar();
fileM.add(open);
fileM.add(clear);
fileM.add(exit);
mb.add(fileM);
textpane=new JTextPane();
scroll=new JScrollPane(textpane);
getContentPane().add(scroll);
getContentPane().addJMenuBar(mb);
}
class OpenAction extends AbstractAction
{
public OpenAction ()
{
super("打开");
}
public void actionPerformed(ActionEvent e )
{
JFileChooser chooser=new JFileChooser();
int state=chooser.showOpenDialog(null);
file=chooser.selectedFile();
if(file!=null&&state==JFileChooser.APPROVE_OPTION)
{
try
{
readStream=new FileInputStream(file);
textpane.read(readStream, this);
readStream.close();
}
catch(IOException ioE){}
}
}
}
class SaveAction extends AbstractAction
{
public SaveAction()
{
super("保存");
}
public void actionPerformed(ActionEvent e )
{
if(file==null)
return;
try{
FileWriter ut = new FileWriter(file);
out.read(textpane.getText());
out.close();
}
catch (IOException ioE)
{}
}
}
class ExitAction extends AbstractAction
{
public ExitAction()