Simple notepad java Source Code project, notepad java source code

Source: Internet
Author: User

Simple notepad java Source Code project, notepad java source code

The Code is as follows:

Import java. awt. *; import java. io. *; import java. awt. datatransfer. *; import java. awt. event. *; public class Main extends Frame implements ActionListener {private static final long serialVersionUID = 1L; TextArea textArea = new TextArea (); MenuBar menuBar = new MenuBar (); menu fileMenu = new Menu ("File"); MenuItem newItem = new MenuItem ("New"); MenuItem openItem = new MenuItem ("Open"); MenuItem saveItem = New MenuItem ("Save"); MenuItem saveAsItem = new MenuItem ("Save As"); MenuItem exitItem = new MenuItem ("Exit "); menu editMenu = new Menu ("Edit"); MenuItem selectItem = new MenuItem ("Select All"); MenuItem copyItem = new MenuItem ("Copy "); menuItem cutItem = new MenuItem ("Cut"); MenuItem pasteItem = new MenuItem ("Paste"); String fileName = null; Toolkit toolKit = Toolkit. getdefatooltoolkit (); Clipboar D clipBoard = toolKit. getSystemClipboard (); private FileDialog openFileDialog = new FileDialog (this, "Open File", FileDialog. LOAD); private FileDialog saveAsFileDialog = new FileDialog (this, "Save File As", FileDialog. SAVE); public Main () {setTitle ("Notepad program-by Jackbase"); setFont (new Font ("Times New Roman", Font. PLAIN, 12); setBackground (Color. white); setSize (400,300); fileMenu. add (newItem); fileMenu. add (o PenItem); fileMenu. addSeparator (); fileMenu. add (saveItem); fileMenu. add (saveAsItem); fileMenu. addSeparator (); fileMenu. add (exitItem); editMenu. add (selectItem); editMenu. addSeparator (); editMenu. add (copyItem); editMenu. add (cutItem); editMenu. add (pasteItem); menuBar. add (fileMenu); menuBar. add (editMenu); setMenuBar (menuBar); add (textArea); addWindowListener (new WindowAdapter () {public void windowClos Ing (receivwevent e) {System. exit (0) ;}}); newItem. addActionListener (this); openItem. addActionListener (this); saveItem. addActionListener (this); saveAsItem. addActionListener (this); exitItem. addActionListener (this); selectItem. addActionListener (this); copyItem. addActionListener (this); cutItem. addActionListener (this); pasteItem. addActionListener (this);} public void actionreceivmed (ActionEvent e ){// Listen to event Object eventSource = e. getSource (); if (eventSource = newItem) {textArea. setText ("");} else if (eventSource = openItem) {openFileDialog. show (); fileName = openFileDialog. getDirectory () + openFileDialog. getFile (); if (fileName! = Null) readFile (fileName);} else if (eventSource = saveItem) {if (fileName! = Null) writeFile (fileName);} else if (eventSource = saveAsItem) {saveAsFileDialog. show (); fileName = saveAsFileDialog. getDirectory () + saveAsFileDialog. getFile (); if (fileName! = Null) writeFile (fileName);} else if (eventSource = selectItem) {textArea. selectAll ();} else if (eventSource = copyItem) {String text = textArea. getSelectedText (); StringSelection selection = new StringSelection (text); clipBoard. setContents (selection, null);} else if (eventSource = cutItem) {String text = textArea. getSelectedText (); StringSelection selection = new StringSelection (text); clipBoard. setContents (selection, null); textArea. replaceRange ("", textArea. getSelectionStart (), textArea. getSelectionEnd ();} else if (eventSource = pasteItem) {Transferable contents = clipBoard. getContents (this); if (contents = null) return; String text; text = ""; try {text = (String) contents. getTransferData (DataFlavor. stringFlavor);} catch (Exception exception) {} textArea. replaceRange (text, textArea. getSelectionStart (), textArea. getSelectionEnd ();} else if (eventSource = exitItem) {System. exit (0) ;}} public void readFile (String fileName) {// Read File Processing try {file File = new file (fileName); FileReader readIn = new FileReader (File ); int size = (int) file. length (); int charsRead = 0; char [] content = new char [size]; while (readIn. ready () charsRead + = readIn. read (content, charsRead, size-charsRead); readIn. close (); textArea. setText (new String (content, 0, charsRead);} catch (IOException e) {System. out. println ("Error opening file") ;}} public void writeFile (String fileName) {// try {File file File = new file (fileName ); fileWriter writeOut = new FileWriter (file); writeOut. write (textArea. getText (); writeOut. close ();} catch (IOException e) {System. out. println ("Error writing file") ;}@suppresswarnings ("deprecation") public static void main (String [] args) {Frame frame = new Main (); // create an object frame. show (); // object display }}

Running result
<Ignore_js_op>

Http://java.662p.com/thread-2217-1-2.html

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.