Java-simple notepad

Source: Internet
Author: User

JDK Version: 1.7.0

Source Code: [java] import java. io. *; import java. awt. *; import java. awt. event. *;/*** The Main Window * @ author Neo Smith */class PadFrame extends Frame {private MenuBar mb; private Menu menuFile; private Menu menuEdit; private MenuItem [] miFile; private TextArea ta; final private Frame frame = this;/*** The inner class * Message Handle */class EventExit implements ActionListener {public vo Id actionreceivmed (ActionEvent e) {System. exit (0) ;}} class SystemExit extends WindowAdapter {public void windowClosing (WindowEvent e) {System. exit (0) ;}} class EventMenuClose implements ActionListener {public void actionreceivmed (ActionEvent e) {ta. setText (null) ;}} class EventOpenFile implements ActionListener {public void actionreceivmed (ActionEvent e) {// Create the OpenFile Dialog Fi LeDialog dlg = new FileDialog (frame, "Open Files", FileDialog. LOAD); dlg. show (); String strPath; if (strPath = dlg. getDirectory ())! = Null) {// get the full path of the selected file strPath + = dlg. getFile (); // open the file try {FileInputStream FCM = new FileInputStream (strPath); BufferedInputStream bis = new BufferedInputStream (FCM); byte [] buf = new byte [3000]; int len = bis. read (buf); ta. append (new String (buf, 0, len); bis. close ();} catch (Exception ex) {ex. printStackTrace () ;}}}/*** Construction Method * Adding Menu and TextArea components * @ param strTitle */public PadFrame (String strTitle) {super (strTitle); this. setLocation (400,200); this. setSize (900,630); // Create the Menu Bar mb = new MenuBar (); menuFile = new Menu ("File"); menuEdit = new Menu ("Edit "); miFile = new MenuItem [] {new MenuItem ("Open"), new MenuItem ("Close"), new MenuItem ("Exit")}; this. setMenuBar (mb); mb. add (menuFile); mb. add (menuEdit); for (int I = 0; I <miFile. length; ++ I) {menuFile. add (miFile [I]);} // Add event handle setMenuEventHandle (new EventExit (), "File", 2); setMenuEventHandle (new EventOpenFile (), "File ", 0); setMenuEventHandle (new EventMenuClose (), "File", 1); this. addWindowListener (new SystemExit (); // add the TextArea component ta = new TextArea (30, 30); this. add (ta);} public void setMenuEventHandle (ActionListener al, String strMenu, int index) {if (strMenu = "File") {miFile [index]. addActionListener (al) ;}} public int getMenuItemAmount (String strMenu) {if ("File" = strMenu) {return miFile. length;} return-1;} public static void main (String [] args) {PadFrame f = new PadFrame ("NotePad"); f. show ();}}

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.