Java implementation simple notepad

Source: Internet
Author: User


Including opening, saving, copying, pasting, and cutting files, all functions are implemented on the menu bar.



Source code:


 
import java.awt. *;
import java.awt.event. *;
import java.io. *;
import javax.swing. *;
import javax.swing.event. *;
import javax.swing.filechooser.FileFilter;
import static java.awt.event.InputEvent. *;
import java.awt.datatransfer. *;
import java.util.Enumeration;
import java.util.Hashtable;
/ * import javax.swing. *;
import javax.swing.filechooser.FileFilter;

import java.awt.event. *;
import java.awt. *;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; * /

class jishiben
{
  public static void main (String args [])
  {
     TextEdit TE = new TextEdit ("Notepad");
  }
}
class TextEdit extends Frame implements ActionListener
{
   MenuBar m;
   Menu m1, m2;
   MenuItem xinjian, dakai, baocun, tuichu, jianqie, fuzhi, zhantie;
   TextArea text;
   String filename;
   FileDialog openFD, saveFD;
   BufferedReader in;
   FileReader read;
   BufferedWriter out;
   FileWriter writer;
   Clipboard cb;
   TextEdit (String s)
   {
     super (s);
     m = new MenuBar ();
     m1 = new Menu ("File");
     xinjian = new MenuItem ("New");
     dakai = new MenuItem ("Open");
     baocun = new MenuItem ("Save");
     tuichu = new MenuItem ("Exit");
     m2 = new Menu ("Edit");
     jianqie = new MenuItem ("Cut");
     fuzhi = new MenuItem ("Copy");
     zhantie = new MenuItem ("Paste");
     text = new TextArea ();
     openFD = new FileDialog (this, "Open", FileDialog.LOAD);
     saveFD = new FileDialog (this, "Save", FileDialog.SAVE);
     filename = "NoName";
     
     m1.add (xinjian);
     m1.addSeparator ();
     m1.add (dakai);
     m1.addSeparator ();
     m1.add (baocun);
     m1.addSeparator ();
     m1.add (tuichu);
     m2.add (jianqie);
     m2.addSeparator ();
     m2.add (fuzhi);
     m2.addSeparator ();
     m2.add (zhantie);
     m.add (m1);
     m.add (m2);
     
     // Key part, no memory is applied for cb, the following cb operation error
    cb = new Clipboard ("nothing");
     //
     
     setMenuBar (m);
     setSize (300,400); setVisible (true);
     add (text, "Center");
     
     xinjian.addActionListener (this);
     dakai.addActionListener (this);
     baocun.addActionListener (this);
     tuichu.addActionListener (this);
     jianqie.addActionListener (this);
     fuzhi.addActionListener (this);
     zhantie.addActionListener (this);
     addWindowListener (new WindowAdapter ()
                      {
                        public void windowClosing (WindowEvent e)
                        {
                          System.exit (0);
                        }
                      });
    }
    
    public void actionPerformed (ActionEvent e)
    {
      if (e.getSource () == xinjian)
       {
         text.setText ("");
       }
      if (e.getSource () == dakai)
       {
        openFD.show ();
        String s;
        filename = openFD.getDirectory () + openFD.getFile ();
        if (filename! = null)
         {
          try
           {
            File file = new File (filename);
            read = new FileReader (file);
            in = new BufferedReader (read);
            while ((s = in.readLine ())! = null)
                text.append (s + '\ n');
            in.close ();
            read.close ();
           }
            catch (IOException e2) {}
         }
       }
      
      if (e.getSource () == baocun)
      {
        saveFD.show ();
        filename = saveFD.getDirectory () + saveFD.getFile ();
        if (filename! = null)
        {
         try
           {
            File file = new File (filename);
            writer = new FileWriter (file);
            out = new BufferedWriter (writer);
            out.write (text.getText (), 0, (text.getText ()). length ());
            
            out.close ();
            writer.close ();
           }
          catch (IOException e2) {}
        }
      }
      if (e.getSource () == tuichu)
      {
        System.exit (0);
      }
      
      
      
      
     if (e.getSource () == jianqie)
      {
      // There is no cut method in class text, you cannot use text.cut
        String s = text.getSelectedText ();
        StringSelection select = new StringSelection (s);
        cb.setContents (select, null);
        text.replaceRange ("", text.getSelectionStart (), text.getSelectionEnd ());
      }
      if (e.getSource () == fuzhi)
      {
      // Same as above, there is no copy method
        String s = text.getSelectedText ();
        StringSelection select = new StringSelection (s);
        cb.setContents (select, null);
      }
      if (e.getSource () == zhantie)
      {
      // Same as above, no paste method
           String s = "";
          Transferable t = cb.getContents (null);
          try
          {
                if (t! = null
                    && t.isDataFlavorSupported (DataFlavor.stringFlavor))
                {
                        // Because there are many kinds of information in the original clipboard, such as text, pictures, files, etc.
                        // First determine whether the data that can be transmitted is the text, and if so, get the text
                        
                    s = (String) t.getTransferData (DataFlavor.stringFlavor);
                        // Similarly, because the DataFlavor in Transferable is of multiple types,
                        // So pass the DataFlavor parameter, which specifies what type of Data to get.
                        //System.out.println(s);
}
           }
           catch (UnsupportedFlavorException ex)
           {
                 ex.printStackTrace ();
           }
           catch (IOException ex)
           {
                 ex.printStackTrace ();
           }
          
           text.insert (s, text.getCaretPosition ());
          
       }
      
     }
  
   
}
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.