import java.awt.*;
import java.awt.event.*;
import java.io.*; public class NoteDemo { private Frame fr;
private TextArea ta;
private MenuBar bar;
private Menu m;
private MenuItem mi1,mi2,mi3,mi4,mi5;
private Font font;
private Dialog dia;
private Button but1,but2; private FileDialog fd1,fd2; private File file = null; NoteDemo()
{
init();
} public void init()
{
fr = new Frame("MyNote by__sho");
ta = new TextArea();
bar = new MenuBar();
m = new Menu("File");
mi1 = new MenuItem("New");
mi2 = new MenuItem("Open");
mi3 = new MenuItem("Save");
mi4 = new MenuItem("Save As");
mi5 = new MenuItem("Exit"); but1 = new Button("是");
but2 = new Button("否"); fd1 = new FileDialog(fr,"File",FileDialog.LOAD );
fd2 = new FileDialog(fr,"File",FileDialog.SAVE ); font = new Font("Monospaced" ,Font.BOLD,20);//新建一个字体对象 dia = new Dialog(fr,"是否保存?",true); dia.setLayout(new FlowLayout());
dia.setBounds(530, 280, 140, 100); fr.setBounds(200, 80, 700, 600);
fr.add(ta);
fr.setFont(font);//设置字体 m.add(mi1);
m.add(mi2);
m.add(mi3);
m.add(mi4);
m.add(mi5);
bar.add(m); fr.setMenuBar(bar); dia.add(but1);
dia.add(but2); myEvent(); fr.setVisible(true); } public void myEvent()
{
fr.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//New
mi1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ta.setText("");
}
});
//Open
mi2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fd1.setVisible(true);
String dirPath = fd1.getDirectory();//返回次对话框的目录
String fileName = fd1.getFile();//获取此文件对话框的选定文件
if(dirPath==null&&fileName==null)
return ;//下面的程序不运行
ta.setText("");
file = new File(dirPath,fileName);
try
{
BufferedReader bufr = new BufferedReader(new FileReader(file));
String str = null;
while((str = bufr.readLine())!=null)
{
ta.append(str +"\r\n");
}
bufr.close();//资源必须关闭
}
catch(IOException ex)
{
throw new RuntimeException("流无法读取");
}
}
});
//Save
mi3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
saveMethod();
}
});
mi4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fd2.setVisible(true);
String dirPath = fd2.getDirectory();//返回次对话框的目录
String fileName = fd2.getFile();//获取此文件对话框的选定文件
if(dirPath==null&&fileName==null)//判断是否取消
return ;//下面的程序不运行
// ta.setText(" ");
file = new File(dirPath,fileName); try
{
BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
String str = ta.getText();
bufw.write(str);
bufw.close();
}
catch(IOException ed)
{
throw new RuntimeException("写入流出现异常");
}
}
});
//Exit
mi5.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str = ta.getText();
if(str.equals(""))
System.exit(0);
else
{
dia.setVisible(true);
}
}
}); but1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
saveMethod();
}
}); but2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}); ta.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
// ctrl和S同时按下
if((e.isControlDown())&&(e.getKeyCode() == e.VK_S))
{
saveMethod();
}
}
});
} public void saveMethod()
{
if(file==null)
{
//文件为空,那么文件对话框就会跳出来
fd2.setVisible(true);
String dirPath = fd2.getDirectory();//返回次对话框的目录
String fileName = fd2.getFile();//获取此文件对话框的选定文件
if(dirPath==null&&fileName==null)//判断是否取消
return ;//下面的程序不运行
// ta.setText(" ");
file = new File(dirPath,fileName);
} try
{
BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
String str = ta.getText();
bufw.write(str);
bufw.close();
}
catch(IOException ed)
{
throw new RuntimeException("写入流出现异常");
}
} public static void main(String[] args)
{
new NoteDemo();
}
}