GUI练习:文件的打开保存
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyMenuDemo {
private Frame f;
private MenuBar mb;
private TextArea ta;
private Menu m;
private MenuItem closeItem,openItem,saveItem;
private FileDialog openDia,saveDia;
private File file;
public MyMenuDemo()
{
init();
}
public void init()
{
f=new Frame("my window");
f.setBounds(200, 100, 500, 400);
ta=new TextArea();
mb=new MenuBar();
m=new Menu("文件");
//subMenu=new Menu("子菜单");
openItem=new MenuItem("打开");
saveItem=new MenuItem("保存");
closeItem=new MenuItem("关闭");
mb.add(m);
m.add(openItem);
m.add(saveItem);
m.add(closeItem);
//subMenu.add(subItem);
f.add(ta);
f.setMenuBar(mb);
f.setVisible(true);
myEvent();
}
public void myEvent()
{
openItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
openDia.setVisible(true);
String pathDir=openDia.getDirectory();
String fileName=openDia.getFile();
if(pathDir==null || fileName==null)
return ;
file=new File(pathDir,fileName);
try {
BufferedReader bufr=new BufferedReader(new FileReader(file));
ta.setText(null);
String line=null;
while((line=bufr.readLine())!=null)
{
ta.append(line+"\r\n");
}
bufr.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
throw new RuntimeException("读取失败");
}
}
});
saveItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
saveDia.setVisible(true);
if(file==null)
{
String pathDir=saveDia.getDirectory();
String fileName=saveDia.getFile();
if(pathDir==null || fileName==null)
return ;
file=new File(pathDir,fileName);
}
try {
BufferedWriter bufw=new BufferedWriter(new FileWriter(file));
String text=ta.getText();
bufw.write(text);
bufw.flush();
bufw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
closeItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyMenuDemo();
}
}
程序运行,但是点击打开保存条目时报错如下:
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at MyMenuDemo
at java.awt.EventQueue
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain
at java.awt.EventQueue
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
查找54行openDia,未对其进行实例化,在第27、28行加入
openDia = new FileDialog(f);
saveDia = new FileDialog(f);
再次运行,程序正确,可以对其进行正确文件打开关闭以及保存操作。
1.点击打开:
2.选择1.txt文件打开:
3.修改文档内容如下:
4.点击保存之后,直接打开1.txt文件如下:
修改保存成功!!!!