Java Notepad program __java

Source: Internet
Author: User
Tags save file

Import java.awt.*;
Import java.awt.event.*;
Import java.lang.string.*;
Import java.awt.datatransfer.*; Data transfer
Import java.util.*;

public class Notepad {
public static void Main (String args[]) {
Mymenuframe MF = new Mymenuframe (); Defining the main class
Mf.setsize (New Dimension (300, 200)); Specify the initial size of the window with the SetSize () method
Mf.setvisible (TRUE);
}
}

//Definition Window
Class Mymenuframe extends Frame implements ActionListener {
 clipboard Clipboard;   &nb sp;       //defines the Clipboard object
 filedialog filedialog_save, filedialog_load;     //Definition File dialog box
 menubar m_menubar;             //define menu bars
 menu menufile, Menuedit, Menustyle, Menusearch, Menuhelp;  //define menu Items
  MenuItem mi_file_new, Mi_file_open, Mi_file_save, Mi_file_savaas,
   mi_file_close, Mi_File_Exit, Mi_edit_ce, Mi_edit_copy, Mi_edit_cut,
   mi_edit_paste, Mi_edit_style, Mi_Style_Font, Mi_Style_ Auto,
   mi_search_sphere, Mi_help_helptopic, mi_help_about;
 textarea text;

 mymenuframe ()             //construction method
  {
  super (Notepad);            //Specify window caption
  text = new TextArea (20, 20);
  add (text);
  clipboard = null;
  clipboard = Gettoolkit (). Getsystemclipboard ();    //Get system Clipboard

  filedialog_save = new FileDialog (This, Save File dialog box, Filedialog.save);
  filedialog_save.setvisible (FALSE);
  filedialog_load = new FileDialog (This, open File dialog box, filedialog.load);
  filedialog_load.setvisible (FALSE);
  m_menubar = new MenuBar ();
  menufile = new Menu ("file");//Create a submenu item, create a menu subkey, and initialize
  menuitem mi_file_new = new MenuItem ("new") ;
  menuitem mi_file_open = new MenuItem ("open");
  menuitem mi_file_save = new MenuItem ("Save");
  menuitem mi_file_saveas = new MenuItem ("Save As");
  menuitem mi_file_close = new MenuItem ("Off");
  menuitem mi_file_exit = new MenuItem ("Exit");
  //mi_file_exit.setshortcut (New Menushortcut (´x´));//Set shortcut key
  mi _file_open.setactioncommand ("open");//Simplify
  mi_file_new.setactioncommand ("new");

Mi_file_exit.setactioncommand ("Exit");

Mi_file_new.addactionlistener (this);//Make menu children respond to action events
Mi_file_open.addactionlistener (this);
Mi_file_save.addactionlistener (this);
Mi_file_saveas.addactionlistener (this);
Mi_file_close.addactionlistener (this);
Mi_file_exit.addactionlistener (this);
Menufile.add (Mi_file_new)//Add menu items to menu item
Menufile.add (Mi_file_open);
Menufile.add (Mi_file_save);
Menufile.add (Mi_file_saveas);
Menufile.add (Mi_file_close);
Menufile.addseparator ()//Add a transverse split line
Menufile.add (Mi_file_exit);

M_menubar.add (Menufile)//Add menu item
Menuedit = new Menu ("edit");
Mi_edit_ce = new MenuItem ("undo");
Mi_edit_copy = new MenuItem ("copy");
Mi_edit_cut = new MenuItem ("cut");
Mi_edit_paste = new MenuItem ("paste");
Mi_edit_copy.setactioncommand ("copy");
Mi_edit_cut.setactioncommand ("cut");
Mi_edit_paste.setactioncommand ("paste");

Mi_edit_ce.addactionlistener (this);
Mi_edit_copy.addactionlistener (this);
Mi_edit_cut.addactionlistener (this);
Mi_edit_paste.addactionlistener (this);
Menuedit.add (MI_EDIT_CE);
Menuedit.add (mi_edit_copy);
Menuedit.add (Mi_edit_cut);
Menuedit.add (Mi_edit_paste);
M_menubar.add (Menuedit);

Menustyle = new Menu ("format");
Mi_style_auto = new MenuItem ("Auto wrap");
Mi_style_font = new MenuItem ("Font");
Mi_style_auto.addactionlistener (this);
Mi_style_font.addactionlistener (this);
Menustyle.add (Mi_style_auto);
Menustyle.add (Mi_style_font);
M_menubar.add (Menustyle);
Menusearch = new Menu ("View");
Mi_search_sphere = new MenuItem ("status bar");
Mi_search_sphere.addactionlistener (this);
Menusearch.add (Mi_search_sphere);
M_menubar.add (Menusearch);
Menuhelp = new Menu ("Help");
Mi_help_helptopic = new MenuItem ("Help topic");
Mi_help_about = new MenuItem ("About Notepad");
Mi_help_helptopic.addactionlistener (this);
Mi_help_about.addactionlistener (this);
Menuhelp.add (Mi_help_helptopic);
Menuhelp.add (mi_help_about);
M_menubar.add (MENUHELP);
Setmenubar (M_menubar);//Add menu bar to menu
Addwindowlistener (New Windowadapter ()/Monitor window (Close window)
{
public void windowclosing (WindowEvent e) {
System.exit (0);
}
});
SetVisible (TRUE);
Validate (); If something else is loaded, it can be used to display the component

}

public void actionperformed (ActionEvent e) {
if (e.getactioncommand () = = "new") {
Text.settext (null);/empty
}
if (e.getactioncommand () = = "Open") {
Filedialog_load.setvisible (TRUE);//Make file Dialog visible
}
if (e.getactioncommand () = = "Save") {
Filedialog_save.setvisible (TRUE);//Make file Dialog visible
}
if (E.getactioncommand () = "Copy") {
String s = text.getselectedtext ();//Copy to Clipboard
StringSelection Text1 = new StringSelection (s);//Drag mouse to select text
Clipboard.setcontents (Text1, NULL);
}
if (e.getactioncommand () = = "Cut") {
String S1 = Text.getselectedtext ();//Cut to Clipboard
StringSelection Text1 = new StringSelection (s1);//Drag mouse to select text
Clipboard.setcontents (Text1, NULL);
Int J = Text.getselectionstart ();
int k = Text.getselectionend ();
Text.replacerange ("", J, k);//Delete selected text from text
}
if (E.getactioncommand () = "paste")//paste data from the Clipboard
{
Transferable transferable = clipboard.getcontents (this);
DataFlavor DataFlavor = Dataflavor.stringflavor;
if (transferable.isdataflavorsupported (DataFlavor)) {
try//handles occurrence of exception events (type not compliant)
{
String s3 = (string) transferable.gettransferdata (DataFlavor);
Text.insert (S3, Text.getcaretposition ());
catch (Exception E1) {
}
}
}
if (e.getactioncommand () = = "Exit") {
Dispose ();
System.exit (0);
}

}
}

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.