A simple notepad. Version V1.0
Supported: File Creation, opening, saving, read-only, and exit (including shortcut keys)
Code:
- Package myclass. notepad;
- Import java. AWT. frame;
- Import java. AWT. headlessexception;
- Import java. AWT. event .*;
- Import java. AWT. menubar;
- Import java. AWT. Menu;
- Import java. AWT. menuitem;
- Import java. AWT. menushortcut;
- Import java. AWT. checkboxmenuitem;
- Import java. AWT. filedialog;
- Import java. Io. fileinputstream;
- Import java. Io .*;
- Import java. AWT. textarea;
- /**
- * <P> title: notepad </P>
- * <P> Description: simple Notepad program </P>
- * <P> copyright: Copyright (c) 2010 </P>
- * <P> company: new core </P>
- * @ Author not attributable
- * @ Version 1.0
- */
- Public class notepad {
- Public static string Path = NULL;
- Public static void main (string [] ARGs) throws headlessexception {
- Final frame = new frame ("simple Notepad ");
- Frame. setsize (800,600 );
- Frame. setlocation (200,200 );
- Frame. addwindowlistener (New windowadapter (){
- Public void windowclosing (windowevent e ){
- System. Exit (0 );
- }
- });
- Menubar bar = new menubar ();
- Menu = new menu ("file", true );
- Menu. addseparator ();
- Menu menu1 = new menu ("edit", true );
- Menu1.addseparator ();
- Menu more = new menu ("more ");
- Final textarea TA = new textarea ();
- Menushortcut MS = new menushortcut (keyevent. vk_s, false );
- Menushortcut ms1 = new menushortcut (keyevent. vk_o, false );
- Menushortcut MS2 = new menushortcut (keyevent. vk_f4, false );
- Menuitem MI = new menuitem ("new ");
- Mi. addactionlistener (New actionlistener (){
- Public void actionreceivmed (actionevent e ){
- Ta. settext ("");
- }
- });
- Menuitem Mi1 = new menuitem ("open", ms1 );
- Final checkboxmenuitem CM = new checkboxmenuitem ("read-only", false );
- Cm. additemlistener (New itemlistener (){
- Public void itemstatechanged (itemevent e ){
- If (CM. getstate ()){
- Ta. seteditable (false );
- } Else {
- Ta. seteditable (true );
- }
- }
- });
- Mi1.addactionlistener (New actionlistener (){
- Public void actionreceivmed (actionevent e ){
- Filedialog FD = new filedialog (frame, "Open File", filedialog. load );
- FD. Show ();
- // System. Out. println (FD. GetFile ());
- Path = FD. getdirectory () + FD. GetFile ();
- // System. Out. println (PATH );
- If (path! = NULL ){
- Try {
- Fileinputstream is = new fileinputstream (PATH );
- Byte B [] = new byte [10*1024];
- Int Len = is. Read (B );
- If (CM. getstate ()){
- Ta. seteditable (false );
- }
- // Clear the previous content
- Ta. settext ("");
- Ta. append (new string (B, 0, Len ));
- }
- Catch (filenotfoundexception ex ){
- System. Out. println ("this file cannot be found ");
- }
- Catch (ioexception ex1 ){
- }
- }
- }
- });
- Menuitem separa = new menuitem ("-");
- Menuitem mi2 = new menuitem ("save", MS );
- Mi2.addactionlistener (New actionlistener (){
- Public void actionreceivmed (actionevent e ){
- String A = TA. gettext ();
- If (path! = NULL ){
- Bufferedwriter WRI = NULL;
- Try {
- WRI = new bufferedwriter (New filewriter (PATH ));
- WRI. Write ();
- WRI. Close ();
- }
- Catch (ioexception ex1 ){
- }
- } Else {
- Filedialog FD = new filedialog (frame, "save file", filedialog. Save );
- FD. Show ();
- String path1 = FD. getdirectory () + FD. GetFile ();
- Try {
- Bufferedwriter WRI = new bufferedwriter (New filewriter (path1 + ". txt "));
- WRI. Write ();
- WRI. Close ();
- }
- Catch (ioexception ex ){
- }
- }
- }
- });
- Menuitem MI3 = new menuitem ("copy ");
- Menuitem mi4 = new menuitem ("Paste ");
- Menuitem MI5 = new menuitem ("cut ");
- Menuitem MI7 = new menuitem ("Save ");
- Menuitem MI8 = new menuitem ("page settings ");
- Menuitem mi9 = new menuitem ("print ");
- Menuitem mi10 = new menuitem ("quit", MS2 );
- Mi10.addactionlistener (New actionlistener (){
- Public void actionreceivmed (actionevent e ){
- System. Exit (0 );
- }
- })
- );
- Menu. Add (MI );
- Menu. Add (Mi1 );
- Menu. Add (separa );
- Menu. Add (mi2 );
- Menu. Add (CM );
- Menu. Add (more );
- Menu. Add (mi10 );
- Menu1.add (MI3 );
- Menu1.add (mi4 );
- Menu1.add (MI5 );
- More. Add (MI7 );
- More. Add (MI8 );
- More. Add (mi9 );
- Bar. Add (menu );
- Bar. Add (menu1 );
- Frame. setmenubar (bar );
- Frame. Add (TA );
- Frame. Show ();
- }
- }