Code---Simulate a simple notepad

Source: Internet
Author: User
Tags readline

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");
}
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.