The code is as follows
Import java.awt.*;import java.io.*;import java.awt.datatransfer.*;import java.awt.event.*;p ublic class Main extends Frame implements ActionListener {private static final long serialversionuid = 1L; TextArea TextArea = new TextArea (); MenuBar MenuBar = new MenuBar (); Menu Filemenu = new Menu ("File"); MenuItem newitem = new MenuItem ("new"); MenuItem OpenItem = new MenuItem ("Open"); MenuItem SaveItem = new MenuItem ("Save"); MenuItem Saveasitem = new MenuItem ("Save as"); MenuItem Exititem = new MenuItem ("Exit"); Menu Editmenu = new Menu ("Edit"); MenuItem SelectItem = new MenuItem ("Select All"); MenuItem CopyItem = new MenuItem ("Copy"); MenuItem Cutitem = new MenuItem ("Cut"); MenuItem Pasteitem = new MenuItem ("Paste"); String fileName = null; Toolkit Toolkit=toolkit.getdefaulttoolkit (); Clipboard Clipboard=toolkit.getsystemclipboard (); Private FileDialog OpenFileDialog = new FileDialog (this, "Open File", filedialog.load); Private FileDialog SaveAsFileDialog = new FileDialog (this, "Save File as", Filedialog.save); Public Main () {settitle ("notepad program-by jackbase"); SetFont (New Font ("Times New Roman", font.plain,12)); SetBackground (Color.White); SetSize (400,300); 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 (New Windowadapter () {public void windowclosing (WindowEvent e) { 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 void actionperformed (ActionEvent e) {//listener event Object EventSource = E.getsource (); if (EventSource = = NewItem) {textarea.settext (""); }else if (EventSource = = OpenItem) {openfiledialog.show (); FileName = Openfiledialog.getdirectory () +openfiledialog.getfile (); if (filename = null) readFile (filename); }else if (EventSource = = SaveItem) {if (filename! = null) WriteFile (filename); }else if (EventSource = = Saveasitem) {saveasfiledialog.show (); 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=new stringselection (text); Clipboard.setcontents (Selection,null); }else if (EventSource = = Cutitem) {String text=textarea.getselectedtext (); StringSelection selection=new stringselection (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; Text= ""; try{text= (String) contents.gettransferdata (Dataflavor.stringflavor); }catch (Exception Exception) {} textarea.replacerange (Text,textarea.getselectionstart (), Textarea.getselectione nd ()); }else if (EventSource = = Exititem) {system.exit (0); }} public void ReadFile (String fileName) {//reads the file processing try{the files file = new file (fileName); FileReader Readin = new FileReader (file); int size = (int) file.length (); int charsread = 0; char[] content = new Char[size]; while (Readin.ready ()) Charsread + = readin.read (content, Charsread, size-charsread); Readin.close (); Textarea.settext (New String (content, 0, charsread)); } catch (IOException e) {System.out.println ("Error opening file"); }} public void WriteFile (String fileName) {///writes to file processing try{files file = new file (fileName); FileWriter writeout = new FileWriter (file); Writeout.write (Textarea.gettext ()); Writeout.close (); } catch (IOException e) {System.out.println ("Error writing file"); }} @SuppressWarnings ("deprecation") public static void main (string[] args) {frame frame = new Main (); Built object Frame.show (); is Object display}}
Run results
<ignore_js_op>
Detailed Description: http://java.662p.com/thread-2217-1-2.html
Simple Notepad program Java source project