import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MumuEdit
{
private Frame editFr;
private MenuBar editMb;
private Menu editMn,editNew;
private MenuItem editSave,editClose,newText,editOpen,editSaveOther;
private TextArea editTa;
private FileDialog openDia,saveDia;
private File file;
MumuEdit()
{
init();
}
public void init()
{
editFr = new Frame();
editFr.setBounds(325,125,800,550);
editMb = new MenuBar();
editMn = new Menu("文件");
editNew = new Menu("新建");
editOpen = new MenuItem("打开");
newText = new MenuItem("文本文件");
editSave = new MenuItem("保存");
editSaveOther = new MenuItem("另存为");
editClose = new MenuItem("退出");
editTa = new TextArea();
editFr.setMenuBar(editMb);
editFr.add(editTa);
editMb.add(editMn);
editMn.add(editNew);
editNew.add(newText);
editMn.add(editOpen);
editMn.add(editSave);
editMn.add(editSaveOther);
editMn.add(editClose);
openDia = new FileDialog(editFr,"打开",FileDialog.LOAD);
saveDia = new FileDialog(editFr,"保存",FileDialog.SAVE);
editEvent();
editFr.setVisible(true);
}
//创建事件处理方法
public void editEvent()
{
//窗口关闭事件
editFr.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//退出菜单事件
editClose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
//打开菜单事件
editOpen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openDia.setVisible(true);
String dirPath = openDia.getDirectory();
String fileName = openDia.getFile();
if(dirPath==null || fileName==null)
return ;
editTa.setText("");
file = new File(dirPath,fileName);
try
{
BufferedReader bufr = new BufferedReader(new FileReader(file));
String line = null;
while ((line=bufr.readLine())!=null)
{
editTa.append(line+"\r\n");
}
}
catch (IOException ex)
{
throw new RuntimeException("读取失败");
}
}
});
//保存菜单事件
editSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(file==null)
{
saveDia.setVisible(true);
String dirPath = saveDia.getDirectory();
String fileName = saveDia.getFile();
if(dirPath==null || fileName==null)
return ;
file = new File(dirPath,fileName);
}
try
{
BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
String text = editTa.getText();
bufw.write(text);
//bufw.flush();
bufw.close();
}
catch (IOException exc)
{
throw new RuntimeException("保存失败!");
}
}
});
//新建菜单事件
editNew.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
editTa.setText("");
}
});
//另存为菜单事件
editSaveOther.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
saveDia.setVisible(true);
String dirPath = saveDia.getDirectory();
String fileName = saveDia.getFile();
if(dirPath==null || fileName==null)
return ;
file = new File(dirPath,fileName);
try
{
BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
String text = editTa.getText();
bufw.write(text);
//bufw.flush();
bufw.close();
}
catch (IOException exc)
{
throw new RuntimeException("保存失败!");
}
}
});
}
public static void main(String[] args)
{
new MumuEdit();
}
}
|