Today to introduce you about how to implement Notepad in Java, is a very good case of learning Java Swing, the following first look at the results of the operation (download the address below):
Here we look at the source code:
[java] View Plain copy import java.awt.*; import java.awt.event.*; import java.text.*; import java.util.*; import java.io.*; import javax.swing.undo.*; import javax.swing.border.*; import javax.swing.*; import javax.swing.text.*; import javax.swing.event.*; import java.awt.datatransfer.*; public class notepad extends jframe implements actionlistener,documentlistener { //Menu JMenu fileMenu,editMenu,formatMenu,viewMenu,helpMenu; //Right-click Popup JPopupMenu popupMenu; jmenuitem popupmenu_undo,popupmenu_cut,popupmenu_copy,popupmenu_paste,popupmenu_delete,popupmenu_selectall; / /"File" menu items jmenuitem filemenu_new,filemenu_open,filemenu_save,filemenu_ saveas,filemenu_pagesetup,filemenu_print,filemenu_exit; //"Edit" menu item jmenuitem editmenu_undo,editmenu_cut,editmenu_copy,editmenu_paste,editmenu_ Delete,editmenu_find,editmenu_findnext,editmenu_replace,editmenu_goto,editmenu_selectall,editmenu_timedate; //"format" menu items JCheckBoxMenuItem formatmenu_linewrap; JMenuItem formatMenu_Font; //"View" menu items JCheckBoxMenuItem viewMenu_Status; //"Help" menu items jmenuitem helpmenu_helptopics , helpmenu_aboutnotepad; //"text" Edit area JTextArea editarea; //status bar label JLabel statuslabel; //system Clipboard Toolkit Toolkit=toolkit.getdefaulttoolkit (); clipboard clipboard= Toolkit.getsystemclipboard (); //Create the Undo Operation Manager (related to the undo operation) protected undomanager undo=new undomanager (); Protected undoableeditlistener undohandler=new undohandler (); //Other variables string oldvalue;//store the original contents of the edit area to compare the text for changes