大家帮忙看看下面的代码有什么问题,为啥我运行的时候老出错了?
import java.awt.*;
import java.awt.event.*;
class MyMenuTest
{
private Frame f;
private MenuBar bar;
private Menu file;
private MenuItem closeItem,saveItem,openItem;
private FileDialog openDia,saveDia;
private TextArea ta;
MyMenuTest()
{
init();
}
private void init()
{
f = new Frame("my window");
f.setBounds(300,100,650,600);
bar = new MenuBar();
file = new Menu("File");
openItem = new MenuItem("Open");
saveItem = new MenuItem("Save");
closeItem = new MenuItem("Exit");
file.add(openItem);
file.add(saveItem);
file.add(closeItem);
bar.add(file);
f.setMenuBar(bar);
openDia = new FileDialog(f,"打开文件",FileDialog.LOAD);
saveDia = new FileDialog(f,"保存文件",FileDialog.SAVE);
ta = new TextArea();
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
closeItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openDia.setVisible(true);
String dirPath = openDia.getDirectory();
String fileName = openDia.getFile();
System.out.println(dirPath+"..."+fileName);
if(dirPath == null || fileName == null)
return;
File file = new File(dirPath,fileName);
try
{
BufferedReader bufr = new BufferedReader(new FileReader(file));
String line = null;
while((line = bufr.readLine())!=null)
{
ta.append(line+"\r\n");
}
}
catch (IOException e)
{
throw new RuntimeException("读取失败!");
}
}
});
}
public static void main(String[] args)
{
new MyMenuTest();
}
}
|