A simple notepad

Source: Internet
Author: User

A simple notepad. Version V1.0

Supported: File Creation, opening, saving, read-only, and exit (including shortcut keys)

Code:
  1. Package myclass. notepad;
  2. Import java. AWT. frame;
  3. Import java. AWT. headlessexception;
  4. Import java. AWT. event .*;
  5. Import java. AWT. menubar;
  6. Import java. AWT. Menu;
  7. Import java. AWT. menuitem;
  8. Import java. AWT. menushortcut;
  9. Import java. AWT. checkboxmenuitem;
  10. Import java. AWT. filedialog;
  11. Import java. Io. fileinputstream;
  12. Import java. Io .*;
  13. Import java. AWT. textarea;
  14. /**
  15. * <P> title: notepad </P>
  16. * <P> Description: simple Notepad program </P>
  17. * <P> copyright: Copyright (c) 2010 </P>
  18. * <P> company: new core </P>
  19. * @ Author not attributable
  20. * @ Version 1.0
  21. */
  22. Public class notepad {
  23. Public static string Path = NULL;
  24. Public static void main (string [] ARGs) throws headlessexception {
  25. Final frame = new frame ("simple Notepad ");
  26. Frame. setsize (800,600 );
  27. Frame. setlocation (200,200 );
  28. Frame. addwindowlistener (New windowadapter (){
  29. Public void windowclosing (windowevent e ){
  30. System. Exit (0 );
  31. }
  32. });
  33. Menubar bar = new menubar ();
  34. Menu = new menu ("file", true );
  35. Menu. addseparator ();
  36. Menu menu1 = new menu ("edit", true );
  37. Menu1.addseparator ();
  38. Menu more = new menu ("more ");
  39. Final textarea TA = new textarea ();
  40. Menushortcut MS = new menushortcut (keyevent. vk_s, false );
  41. Menushortcut ms1 = new menushortcut (keyevent. vk_o, false );
  42. Menushortcut MS2 = new menushortcut (keyevent. vk_f4, false );
  43. Menuitem MI = new menuitem ("new ");
  44. Mi. addactionlistener (New actionlistener (){
  45. Public void actionreceivmed (actionevent e ){
  46. Ta. settext ("");
  47. }
  48. });
  49. Menuitem Mi1 = new menuitem ("open", ms1 );
  50. Final checkboxmenuitem CM = new checkboxmenuitem ("read-only", false );
  51. Cm. additemlistener (New itemlistener (){
  52. Public void itemstatechanged (itemevent e ){
  53. If (CM. getstate ()){
  54. Ta. seteditable (false );
  55. } Else {
  56. Ta. seteditable (true );
  57. }
  58. }
  59. });
  60. Mi1.addactionlistener (New actionlistener (){
  61. Public void actionreceivmed (actionevent e ){
  62. Filedialog FD = new filedialog (frame, "Open File", filedialog. load );
  63. FD. Show ();
  64. // System. Out. println (FD. GetFile ());
  65. Path = FD. getdirectory () + FD. GetFile ();
  66. // System. Out. println (PATH );
  67. If (path! = NULL ){
  68. Try {
  69. Fileinputstream is = new fileinputstream (PATH );
  70. Byte B [] = new byte [10*1024];
  71. Int Len = is. Read (B );
  72. If (CM. getstate ()){
  73. Ta. seteditable (false );
  74. }
  75. // Clear the previous content
  76. Ta. settext ("");
  77. Ta. append (new string (B, 0, Len ));
  78. }
  79. Catch (filenotfoundexception ex ){
  80. System. Out. println ("this file cannot be found ");
  81. }
  82. Catch (ioexception ex1 ){
  83. }
  84. }
  85. }
  86. });
  87. Menuitem separa = new menuitem ("-");
  88. Menuitem mi2 = new menuitem ("save", MS );
  89. Mi2.addactionlistener (New actionlistener (){
  90. Public void actionreceivmed (actionevent e ){
  91. String A = TA. gettext ();
  92. If (path! = NULL ){
  93. Bufferedwriter WRI = NULL;
  94. Try {
  95. WRI = new bufferedwriter (New filewriter (PATH ));
  96. WRI. Write ();
  97. WRI. Close ();
  98. }
  99. Catch (ioexception ex1 ){
  100. }
  101. } Else {
  102. Filedialog FD = new filedialog (frame, "save file", filedialog. Save );
  103. FD. Show ();
  104. String path1 = FD. getdirectory () + FD. GetFile ();
  105. Try {
  106. Bufferedwriter WRI = new bufferedwriter (New filewriter (path1 + ". txt "));
  107. WRI. Write ();
  108. WRI. Close ();
  109. }
  110. Catch (ioexception ex ){
  111. }
  112. }
  113. }
  114. });
  115. Menuitem MI3 = new menuitem ("copy ");
  116. Menuitem mi4 = new menuitem ("Paste ");
  117. Menuitem MI5 = new menuitem ("cut ");
  118. Menuitem MI7 = new menuitem ("Save ");
  119. Menuitem MI8 = new menuitem ("page settings ");
  120. Menuitem mi9 = new menuitem ("print ");
  121. Menuitem mi10 = new menuitem ("quit", MS2 );
  122. Mi10.addactionlistener (New actionlistener (){
  123. Public void actionreceivmed (actionevent e ){
  124. System. Exit (0 );
  125. }
  126. })
  127. );
  128. Menu. Add (MI );
  129. Menu. Add (Mi1 );
  130. Menu. Add (separa );
  131. Menu. Add (mi2 );
  132. Menu. Add (CM );
  133. Menu. Add (more );
  134. Menu. Add (mi10 );
  135. Menu1.add (MI3 );
  136. Menu1.add (mi4 );
  137. Menu1.add (MI5 );
  138. More. Add (MI7 );
  139. More. Add (MI8 );
  140. More. Add (mi9 );
  141. Bar. Add (menu );
  142. Bar. Add (menu1 );
  143. Frame. setmenubar (bar );
  144. Frame. Add (TA );
  145. Frame. Show ();
  146. }
  147. }

 

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.