Java notepad code
Code:
Package qqq; import java. awt. color; import java. awt. font; import java. awt. graphicsEnvironment; import java. awt. insets; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. io. file; import java. io. fileReader; import java. io. fileWriter;/*** @ author he rongwei * Created Date: 2015/11/28 **/import javax. swing. JCheckBoxMenuItem; import javax. swing. JColorChooser; import javax. swing. JFileC Hooser; import javax. swing. JFrame; import javax. swing. JList; import javax. swing. JMenu; import javax. swing. JMenuBar; import javax. swing. JMenuItem; import javax. swing. JOptionPane; import javax. swing. JScrollPane; import javax. swing. JTextArea; import javax. swing. undo. undoManager; // implement the revocation of menu Activity events public class aaa extends JFrame {/*****/UndoManager manager = new UndoManager (); JTextArea text = new JTextArea (); JFi LeChooser jfc = new notepad (); String jsb = "new Notepad"; File file; JMenuBar menu; JMenu File_bar, Edit_bar, Format_bar, View_bar, Help_bar; JMenuItem folder, File_bar_open, File_bar_save, parameters, File_bar_exit; JMenuItem values, Edit_bar_shear, Edit_bar_copy, Edit_bar_paste, Edit_bar_delete; JMenuItem Format_bar_hl, m30, labels, labels, View_bar_about, El P_bar_help;/* file format filter */public class filter extends javax. swing. filechooser. fileFilter {public boolean accept (File file) {String name = file. getName (); name. toString (); // The number in the string is converted to the letter name. toLowerCase (); // the letters in the string are converted to lowercase letters/* the file suffix is .txt and is a directory */if (name. endsWith (". txt ") | file. isDirectory () {return true;} elsereturn false;}/* references the method of the subclass object of a specific subclass. The getDescription () in the class cannot be omitted (), the reason is that the compiler only allows calling methods declared in the class. */public Stri Ng getDescription () {return ". txt ";}}/* Add the menu item JMenu to JMenuBar */public JMenu AddBar (String name, JMenuBar menu) {JMenu jmenu = new JMenu (name); menu. add (jmenu); return jmenu;}/* add menu item JMenuItem to menu JMenu */public JMenuItem AddItem (String name, JMenu menu) {JMenuItem jmenu = new JMenuItem (name); menu. add (jmenu); return jmenu;} aaa aaa1; {setTitle (jsb); // set the window title setBounds (250,250,500,500); // set the border JMenuBar Menu = new JMenuBar (); // Add menu jmenubaris. setJMenuBar (menu); // call this method text. getDocument (). addUndoableEditListener (manager); // the document used to obtain the current validity of the Program/** Font is the Font class in JAVA, and PLAIN is the static constant (static final) in the Font class, indicating: regular Style Constant BOLD *: Constant of BOLD style, ITALIC: Constant of ITALIC style, 14: pound */text. setFont (new Font ("", Font. PLAIN, 14);/* cursor color */text. setCaretColor (Color. gray);/* select the font color */text. setSelectedTextColor (Color. blue);/* select the background color */text. se TSelectionColor (Color. green);/* Whether to wrap */text. setLineWrap (true);/* Whether to wrap the word boundary (blank) */text. setWrapStyleWord (true);/* distance between the border area and the border. The four parameters are top, left, bottom, and right */text. setMargin (new Insets (3, 5, 3, 5);/* enable or disable auto-drag Processing */text. setDragEnabled (true);/* Create a JScrollPane that displays the view component in a single view. You can use a pair of scroll bars to control the view position */add (new JScrollPane (text, JScrollPane. VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane. HORIZONTAL_SCROLLBAR_AS_NEEDED); File_bar = thi S. addBar ("file (F)", menu); Edit_bar = this. addBar ("Edit (E)", menu); Format_bar = this. addBar ("format (O)", menu); View_bar = this. addBar ("View (V)", menu); Help_bar = this. addBar ("Help (H)", menu);/* File Options * // * New Options */File_bar_creat = this. addItem ("new (N) Ctrl + N", File_bar); File_bar_creat.addActionListener (new ActionListener () {// @ Overridepublic void actionreceivmed (ActionEvent arg0) {// TODO automatically generated method of the stub text. setText (""); });/* Open option */File_bar_open = this. addItem ("open (O) Ctrl + O", File_bar); File_bar_open.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent event) {try {jfc. setCurrentDirectory (new File (". "); // set the current directory // jfc. setFileFilter (new filter (); // filter the jfc file. setFileFilter (new filter (); // filter the jfc file. setAcceptAllFileFilterUsed (false); // select all files // jfc. addChoosableFileFilter (new // javax. swing. fi Lechooser. FileFilter (); // The jfc. showOpenDialog (null) type cannot be instantiated. // a "Open File" File selector dialog box is displayed. File = jfc. getSelectedFile (); // get the selected directory jsb = file. getName (); // get the directory name setTitle (jsb); // display the directory name int length = (int) (jfc. getSelectedFile ()). length (); char [] ch = new char [length]; FileReader fr = new FileReader (file); fr. read (ch); jsb = new String (ch); text. setText (jsb. trim (); // obtain the value of the object field, convert it to the string type, and remove the leading and trailing spaces ~~ ToString () is the method for converting to a string // Trim () is to remove spaces on both sides} catch (Exception e) {JOptionPane. showMessageDialog (null, e) ;}});/* save option = (1) if the file is empty, create a new directory to save; (2) if the current file exists, directly save */File_bar_save = this. addItem ("Save (S) Ctrl + O", File_bar); File_bar_save.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent event) {if (file = null) {try {jfc = new JFileChooser (); jfc. setCurrentDirectory (null); jsb = JOptionPane. sh OwInputDialog ("Enter the file name:") + ". txt ";/** setSelectedFile returns the files selected in the dialog box, but if the type of the dialog box is showSaveDialog, * the returned value is the file you want to save. This file may exist, it may not exist, that is, the file name you entered in the dialog box. * now that you know the file, if it does not exist, create a new one and write data to the file, in this way, you can save */jfc. setSelectedFile (new File (jsb); jfc. setFileFilter (new filter (); int temp = jfc. showSaveDialog (null); if (temp = jfc. APPROVE_OPTION) {if (file! = Null) file. delete (); file = new File (jfc. getCurrentDirectory (), jsb); file. createNewFile (); FileWriter fw = new FileWriter (file); fw. write (text. getText (); fw. close () ;}} catch (Exception e) {JOptionPane. showMessageDialog (null, e) ;}} else {try {FileWriter fw = new FileWriter (file); fw. write (text. getText (); fw. close ();} catch (Exception e) {JOptionPane. showMessageDialog (null, e) ;}}});/* Save As option */File_bar_othe Rsave = this. addItem ("Save as ()... ", File_bar); File_bar_othersave.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent event) {// file fw = new file (); jfc = new JFileChooser (); jfc. setCurrentDirectory (new File (". "); try {if (file = null) {jsb = JOptionPane. showInputDialog ("Enter the file name:") + ". txt ";} elsejsb = file. getName (); jfc. setSelectedFile (new File (jsb); jfc. setFileFilter (new fil Ter (); int temp = jfc. showSaveDialog (null); if (temp = jfc. APPROVE_OPTION) {if (file! = Null) file. delete (); file = new File (jfc. getCurrentDirectory (), jsb); file. createNewFile (); FileWriter fw = new FileWriter (file); fw. write (text. getText (); fw. close () ;}} catch (Exception e) {JOptionPane. showMessageDialog (null, e) ;}});/* Add the default size separator to the end of the toolbar. */File_bar.addSeparator ();/* exit option + exit prompt */File_bar_exit = this. addItem ("exit (X)", File_bar); File_bar_exit.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent event) {int state = JOptionPane. showConfirmDialog (aaa1, "Are you sure you want to exit? Before exiting, make sure that your file has been saved "); if (state = JOptionPane. OK _OPTION) System. exit (0) ;}});/* edit option * // * undo option */Edit_bar_Revoke = this. addItem ("undo (U) Ctrl + Z", Edit_bar); Edit_bar_Revoke.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent event) {if (manager. canUndo () manager. undo () ;}});/* cut option */Edit_bar_shear = this. addItem ("cut (T) Ctrl + X", Edit_bar); Edit_bar_shear.addActionListener (new ActionLi Stener () {public void actionreceivmed (ActionEvent event) {text. cut () ;}});/* Copy option */Edit_bar_copy = this. addItem ("Copy (C) Ctrl + C", Edit_bar); Edit_bar_copy.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent event) {text. copy () ;}});/* Paste option */Edit_bar_paste = this. addItem ("paste (P) Ctrl + V", Edit_bar); Edit_bar_paste.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent event) {text. paste () ;}});/* delete option = use space to replace the selected items from the beginning to the end */Edit_bar_delete = this. addItem ("Delete (L) Del", Edit_bar); Edit_bar_delete.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent event) {text. replaceRange ("", text. getSelectionStart (), text. getSelectionEnd () ;}});/* Wrap options * // m26 = this. addItem (" (W)", m3); JCheckBoxMenuItem Format_bar_hl = new JCheckBoxMenuItem ("Automatic line feed", true); Format_bar.add (Format_bar_hl); Format_bar_hl.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent event) {/* obtain the file information based on the file name */if (Format_bar_hl.getState () text. setLineWrap (true); elsetext. setLineWrap (false) ;}});/* font options * // ** font format setting options GraphicsEnvironment class describes Java (tm) the application can use the ** GraphicsDevice object and the Font object set */Format_bar_ztxz = this. addItem ("font selection (F)", Form At_bar); Format_bar_ztxz.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent event) {/* obtain the local image environment */GraphicsEnvironment gr = GraphicsEnvironment. getLocalGraphicsEnvironment ();/* font name list box */JList fontnames = new JList (gr. getAvailableFontFamilyNames ();/* JScrollPane manages the view, optional vertical and horizontal scroll bars, and optional row and column heading views */int selection = JOptionPane. showConfirmDialog (null, new JScrollPane (fontnames ), "Select font", JOptionPane. OK _CANCEL_OPTION); // Object selectedFont = fontnames. getObject selectedFont = fontnames. getSelectedValue (); // Object selectedFont = fontnames. getSelectedIndex (); if (selection = JOptionPane. OK _OPTION & selectedFont! = Null) {text. setFont (new Font (fontnames. getSelectedValue (). toString (), Font. PLAIN, 20) ;}});/* font color setting options */Format_bar_ztsz = this. addItem ("Color (C)", Format_bar); Format_bar_ztsz.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent event) {color = JColorChooser. showDialog (null, "select text Color", Color. BLACK); text. setForeground (color) ;}}); View_bar_about = this. addItem ("About notepad (About)", View_bar); View_bar_about.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent event) {JOptionPane. showMessageDialog (null, "Notepad \ n development language: JAVA \ n developer: [He rongwei & Li Miao] \ n contact information: [email protected]", "about", JOptionPane. PLAIN_MESSAGE) ;}}); Help_bar_help = this. addItem ("Help options (H)", Help_bar); Help_bar_help.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent event) {JOptionPane. showMessageDialog (null, "For detailed code, go to \ n blog: www.acmerbar.com", "help", JOptionPane. PLAIN_MESSAGE) ;}}); setResizable (true); // whether the form is variable setVisible (true); // whether the form is visible setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE);} public static void main (String args []) {aaa example = new aaa ();}}