Javase based on the AWT Notepad implementation

Source: Internet
Author: User
Tags readfile save file

{Feature Description}
The NoteBook class is a basic class for Notepad, which has the basic functions of Notepad, including:
Open the file, save the file and copy, paste, delete the text and so on.
The program consists of adding a menu bar for a window, adding a menu to the menu bar, adding a menu item to the menu bar, and adding the appropriate listener for each menu item to complete the action. In addition, it also includes reading and saving the contents of the file.

{Instance Diagram}


{Related Code}

 PackageSup.orange.learn;Importjava.awt.*;ImportJava.awt.datatransfer.Clipboard;ImportJava.awt.datatransfer.DataFlavor;Importjava.awt.datatransfer.StringSelection;Importjava.awt.datatransfer.Transferable;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.event.WindowAdapter;Importjava.awt.event.WindowEvent;ImportJava.io.File;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;/*** Created by Re-x on 11/3/14.*/ Public classNoteBookextendsFrameImplementsactionlistener{MenuBar MenuBar=NewMenuBar (); TextArea TextArea=NewTextArea (); //File MenuMenu Filemenu =NewMenu ("File"); MenuItem NewItem=NewMenuItem ("New"); MenuItem OpenItem=NewMenuItem ("Open"); MenuItem SaveItem=NewMenuItem ("Save"); MenuItem Saveasitem=NewMenuItem ("Save as"); MenuItem Exititem=NewMenuItem ("Exit"); //Edit MenuMenu Editmenu =NewMenu ("Edit"); MenuItem SelectItem=NewMenuItem ("Select All"); MenuItem CopyItem=NewMenuItem ("Copy"); MenuItem Cutitem=NewMenuItem ("Cut"); MenuItem Pasteitem=NewMenuItem ("Paste"); String FileName= "NoName.txt"; Toolkit Toolkit=Toolkit.getdefaulttoolkit (); Clipboard Clipboard=Toolkit.getsystemclipboard (); //Create and init Open File Dialog && Save File Dialog    PrivateFileDialog OpenFileDialog =NewFileDialog ( This, "Open File", Filedialog.load); PrivateFileDialog Saveasfiledialog =NewFileDialog ( This, "Save as", Filedialog.save);  PublicNoteBook () {Settitle ("NotePad"); SetFont (NewFont ("Times New Roman", Font.plain, 12));        SetBackground (Color.gray); SetSize (600, 400);        Filemenu.add (NewItem);        Filemenu.add (OpenItem);        Filemenu.addseparator ();        Filemenu.add (SaveItem);        Filemenu.add (Saveasitem);        Filemenu.addseparator ();        Filemenu.add (Exititem);        Editmenu.add (SelectItem);        Editmenu.addseparator ();        Editmenu.add (CopyItem);        Editmenu.add (Cutitem);        Editmenu.add (Pasteitem);        Menubar.add (Filemenu);        Menubar.add (Editmenu);        Setmenubar (MenuBar);        Add (TextArea); Addwindowlistener (NewWindowadapter () {@Override Public voidwindowclosing (windowevent e) {Super. windowclosing (e);                Dispose (); System.exit (0);        }        }); Newitem.addactionlistener ( This); Openitem.addactionlistener ( This); Saveitem.addactionlistener ( This); Saveasitem.addactionlistener ( This); Exititem.addactionlistener ( This); Selectitem.addactionlistener ( This); Copyitem.addactionlistener ( This); Cutitem.addactionlistener ( This); Pasteitem.addactionlistener ( This); }     Public Static voidMain (string[] args) {Frame frame=NewNoteBook (); Dimension screensize= Toolkit.getdefaulttoolkit (). Getscreensize ();//Get screen sizeDimension framesize = Frame.getsize ();//Get frame size        if(Framesize.height >screensize.height) {framesize.height=Screensize.height; }        if(Framesize.width >screensize.width) {framesize.width=Screensize.width; } frame.setlocation (Screensize.width-framesize.width)/2, (screensize.height-framesize.height)/2); Frame.setvisible (true); }     Public voidactionperformed (ActionEvent e) {Object EventSource=E.getsource (); //judge which MenuItem is the source        if(EventSource = =newitem) {Textarea.settext (""); }        Else if(EventSource = =OpenItem) {openfiledialog.setvisible (true); FileName= Openfiledialog.getdirectory () +Openfiledialog.getfile (); if(FileName! =NULL) {readFile (fileName); }        }        Else if(EventSource = =SaveItem) {            if(FileName! =NULL) {WriteFile (fileName); }        }        Else if(EventSource = =Saveasitem) {saveasfiledialog.setvisible (true); FileName= Saveasfiledialog.getdirectory () +Saveasfiledialog.getfile (); if(FileName! =NULL) {WriteFile (fileName); }        }        Else if(EventSource = =SelectItem)        {Textarea.selectall (); }        Else if(EventSource = =CopyItem) {String text=Textarea.getselectedtext (); StringSelection selection=Newstringselection (text); Clipboard.setcontents (Selection,NULL); }        Else if(EventSource = =Cutitem) {String text=Textarea.getselectedtext (); StringSelection selection=Newstringselection (text); Clipboard.setcontents (Selection,NULL); Textarea.replacerange ("", Textarea.getselectionstart (), Textarea.getselectionend ()); }        Else if(EventSource = =Pasteitem) {Transferable Contents= Clipboard.getcontents ( This); if(contents = =NULL) {                return; } String Text= ""; Try{text=(String) contents.gettransferdata (Dataflavor.stringflavor); }            Catch(Exception ex) {ex.printstacktrace ();        } textarea.replacerange (text, Textarea.getselectionstart (), Textarea.getselectionend ()); }        Else if(EventSource = =Exititem) {System.exit (0); }    }     Public voidreadFile (String fileName) {Try{File File=NewFile (fileName); FileReader Readin=Newfilereader (file); intSize = (int) file.length (); intCharsread = 0; Char[] content =New Char[size];  while(Readin.ready ()) {Charsread+ = Readin.read (content, Charsread, size-charsread);            } readin.close (); Textarea.settext (NewString (content, 0, Charsread)); }        Catch(IOException e) {e.printstacktrace (); }    }     Public voidWriteFile (String fileName) {Try{File File=NewFile (fileName); FileWriter Writeout=NewFileWriter (file);            Writeout.write (Textarea.gettext ());        Writeout.close (); }        Catch(IOException e) {e.printstacktrace (); }    }}

Javase based on the AWT Notepad implementation

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.