import javax.swing.*;
import java.awt.FileDialog;
import java.awt.event.*;
import java.util.*; //Date needed
import java.io.*;
public class Area extends JFrame implements ActionListener {
FileDialog open, save; // FileDialog 类显示一个对话框窗口,用户可以从中选择文件
JScrollPane jsp;
JTextArea area;
JMenuBar jmb;
JMenu mFile, mEdit;
JMenuItem fNew, fOpen, fSave, fExit, eCopy, eCut, ePaste, eDate, eCancel;
// 构造函数
public Area() {
super("文本编辑器");
area = new JTextArea("", 24, 40);
jsp = new JScrollPane(area);
jmb = new JMenuBar();
mFile = new JMenu("文件");
mEdit = new JMenu("编辑");
// 向菜单栏中添加菜单
jmb.add(mFile);
jmb.add(mEdit);
// 文件菜单
fNew = new JMenuItem("新建");
fSave = new JMenuItem("保存");
fOpen = new JMenuItem("打开");
fExit = new JMenuItem("退出");
// 编辑菜单
// eCancel=new JMenuItem("撤销");
eCopy = new JMenuItem("复制");
eCut = new JMenuItem("剪切");
ePaste = new JMenuItem("粘贴");
eDate = new JMenuItem("日期/时间");
open = new FileDialog(this, "打开", FileDialog.LOAD);
save = new FileDialog(this, "保存", FileDialog.SAVE);
// public FileDialog(Dialog parent,String title,int mode)
// parent - 对话框的所有者 ,title - 对话框的标题;mode - 对话框的模式,FileDialog.LOAD 或
// FileDialog.SAVE
// LOAD此常量值指示文件对话框窗口的作用是查找要读取的文件。
// SAVE此常量值指示文件对话框窗口的作用是查找要写入的文件。
// 文件菜单的子菜单的处理
mFile.add(fOpen);
mFile.addSeparator(); // 添加分割线
// addSeparator()方法是往菜单、工具条中加入一条横线,实现菜单项及工具条的分组功能
mFile.add(fNew);
mFile.addSeparator();
mFile.add(fSave);
mFile.addSeparator();
mFile.add(fExit);
// 编辑菜单的子菜单的处理
mEdit.add(eCopy);
mEdit.addSeparator();
mEdit.add(eCut);
mEdit.addSeparator();
mEdit.add(ePaste);
mEdit.addSeparator();
mEdit.add(eDate);
this.setJMenuBar(jmb); // public void setJMenuBar(JMenuBar menubar)
// 设置此窗体的菜单栏
this.getContentPane().add(jsp); // public Container getContentPane()
// 返回此窗体的 contentPane 对象
this.setSize(600, 400);
this.setVisible(true); // 设置窗体为可见
fNew.addActionListener(this);
fSave.addActionListener(this);
fOpen.addActionListener(this);
fExit.addActionListener(this);
eCopy.addActionListener(this);
eCut.addActionListener(this);
ePaste.addActionListener(this);
eDate.addActionListener(this);
}
public void actionPerformed(ActionEvent e) //void actionPerformed(ActionEvent e) 发生操作时调用
{
if(e.getSource()==fNew) // java.util 类 EventObject public Object getSource() 返回最初发生 Event 的对象
{
area.setText(" "); //新建文件
}
else if(e.getSource()==fOpen) //打开文件
{
String str;
open.setVisible(true);
try
{
File f1=new File(open.getDirectory(),open.getFile());
FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
area.setText("");
while((str=br.readLine())!=null)area.append(str+"/n");
fr.close();
}
catch(Exception e1)
{}
}
else if(e.getSource()==fSave) //保存文件
{
save.setVisible(true);
try
{
File f1=new File(save.getDirectory(),save.getFile());
FileWriter fw=new FileWriter(f1);
BufferedWriter bw=new BufferedWriter(fw);
String gt=area.getText();
bw.write(gt,0,gt.length());
bw.flush();
fw.close();
}
catch(Exception e2)
{}
}
else if(e.getSource()==eCopy)
{
area.copy(); //复制文件
}
else if(e.getSource()==eCut)
{
area.cut(); //剪切文件
}
else if(e.getSource()==ePaste)
{
area.paste(); //粘贴文件
}
else if(e.getSource()==eDate)
{
Date d=new Date(); //日期
area.append(d.toString());
}
else
{
System.exit(0); //退出
}
}
// 主函数,程序入口
public static void main(String s[]) {
new Area();
}
}