Package Com.practice4;
Import java.awt.*;
Import java.awt.event.*;
Import java.io.*;
public class Mynotepad extends Frame implements actionlistener{
MenuBar MB;
Menu m1,m2;
MenuItem Mi_open,mi_save,mi_saveas,mi_exit;
TextArea Ta;
String sfd= "";
FileDialog Fd_file,fd_save,fd_saveas;
BufferedReader Br=null;
BufferedWriter Bw=null;
Public Mynotepad ()
{
Super ("Notepad");
M1=new Menu ("file");
M2=new menu ("Help");
Mb=new MenuBar ();
Ta=new TextArea ();
Mi_open=new MenuItem ("open");
Mi_save=new MenuItem ("Save");
Mi_saveas=new MenuItem ("Save As");
Mi_exit=new MenuItem ("exit");
Fd_file=new FileDialog (This, "open", filedialog.load);
Fd_save=new FileDialog (This, "save", Filedialog.save);
Mb.add (M1);
Mb.add (m2);
M1.add (Mi_open);
M1.add (Mi_save);
M1.add (Mi_saveas);
M1.add (Mi_exit);
Mi_open.addactionlistener (this);
Mi_save.addactionlistener (this);
Mi_saveas.addactionlistener (this);
Mi_exit.addactionlistener (this);
Addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
System.exit (0);
}
});
This.setmenubar (MB);
Add (TA);
SetBounds (200,200,750,650);
SetVisible (TRUE);
}
public static void Main (string[] args) {
New Mynotepad ();
}
public void actionperformed (ActionEvent e) {
String S=e.getactioncommand ();
if (S.equals ("open"))
{
Fd_file.setvisible (TRUE);
String Stf=fd_file.getfile ();
String std=fd_file.getdirectory ();
if ((stf!=null) && (std!=null)
{
SFD=STF+STD;
OpenFile (SFD);
}
}else if (s.equals ("Save"))
{
if (sfd==null| | Sfd.equals (""))
{
SaveFileAs (SFD);
}
Else
SaveFile (SFD);
}else if (S.equals ("Save As")
{
SaveFileAs (SFD);
}else if (s.equals ("Exit"))
{
System.exit (0);
}
}
public void OpenFile (String s)
{
Ta.settext ("");
Settitle (sfd+ "-Notepad");
try{
Br=new BufferedReader (new FileReader (s));
String St=br.readline ();
while (St!=null)
{
Ta.append (st+ "\ n");
St=br.readline ();
}
}catch (IOException e)
{
E.tostring ();
}finally{
if (br!=null)
try{
Br.close ();
}catch (IOException e)
{
E.tostring ();
}
}
}
public void SaveFile (String s)
{
String St=ta.gettext ();
try{
PrintWriter pw=new PrintWriter (new FileWriter (s));
Pw.println (ST);
}catch (IOException e)
{
E.tostring ();
}finally
{
if (bw!=null)
try{
Bw.close ();
}catch (IOException e)
{
E.tostring ();
}
}
}
public void SaveFileAs (String s)
{
Fd_save.setvisible (TRUE);
String std=fd_save.getdirectory ();
String Stf=fd_save.getfile ();
if ((std!=null) && (stf!=null)
{
SFD=STD+STF;
SaveFile (SFD);
Settitle (sfd+ "-Notepad");
}
}
}