[vc_row][vc_column width=”1/1″][vc_column_text]This post shows a simple text editor created with Java on pcDuino.[/vc_column_text][vc_tour][vc_tab title=”The resulting Text Editor” tab_id=”1395292933-1-67″][vc_column_text]The designed text editor:
[/vc_column_text][/vc_tab][vc_tab title=”Source code” tab_id=”1395292933-2-18″][vc_column_text]
import java.awt *.; import java.awt.event *.; import java.io. *; public class Notepad / * implements ActionListener, MouseListener, MouseMotionListener, WindowListener, ItemListener, KeyListener, TextListener * / { / / Member variables private Frame mainFrame ;/ / main frame private MenuBar mb; / / menu bar private Menu mFile, mEdit, mFormat, mHelp; / / menus : File, Edit , format, help private MenuItem miNew, miOpen, miSave, miSaveAs, miExit ;/ / File menu items: New, Open, Save , Save As , Exit private MenuItem miCut, miCopy, miPaste, miDelete ;/ / Edit menu item : cut, copy , paste, delete private MenuItem miFont, miLowtoCapital, miCapitaltoLow, miEncrypt, miDisencrypt ;/ / Format menu items : Fonts private MenuItem miAboutNotepad ;/ / Help menu item : About Notepad private TextArea ta ;/ / text area String private String tempString ;/ / temporary string , copy and paste the need for storage private boolean textValueChanged = false; private int id_font ;/ / fonts String fileName = "" ;/ / last saved the file name and address / / Constructor public Notepad () { / / Frame mainFrame = new Frame ("Notepad v0.99 by Launching"); mb = new MenuBar (); ta = new TextArea (30, 60); ta.setFont (new Font ("Times New Rome", Font.PLAIN, 15)); ta.setBackground (new Color (0, 250, 200)); / / Menu bar mFile = new Menu (" File" ) ; mEdit = new Menu (" Edit" ) ; mFormat = new Menu (" format t"); mHelp = new Menu (" Help " ) ; / / " File" miNew = new MenuItem (" New" ) ; miOpen = new MenuItem (" Open" ) ; miSave = new MenuItem (" Save" ) ; miSaveAs = new MenuItem (" Save As" ) ; miExit = new MenuItem (" Exit" ) ; / / " Edit" miCut = new MenuItem (" Cut" ) ; miCopy = new MenuItem (" Copy" ) ; miPaste = new MenuItem (" Paste" ) ; miDelete = new MenuItem (" Delete" ) ; / / " Format " miFont = new MenuItem ("Font"); miLowtoCapital = new MenuItem ("Low to Capital"); miCapitaltoLow = new MenuItem ("Capital to Low"); miEncrypt = new MenuItem ("Encrypt"); miDisencrypt = new MenuItem ("Disencrypt"); / / "Help" miAboutNotepad = new MenuItem (" About Notepad"); / / Add File menu item mFile.add (miNew); mFile.add (miOpen); mFile.add (miSave); mFile.add (miSaveAs); mFile.add (miExit); / / Add the Edit menu items mEdit.add (miCut); mEdit.add (miCopy); mEdit.add (miPaste); mEdit.add (miDelete); / / Add formatting menu items mFormat.add (miFont); mFormat.add (miLowtoCapital); mFormat.add (miCapitaltoLow); mFormat.add (miEncrypt); mFormat.add (miDisencrypt); / / Add the Help menu item mHelp.add (miAboutNotepad); / / Add menu menu bar mb.add (mFile); mb.add (mEdit); mb.add (mFormat); mb.add (mHelp); / / Add menu bar frame mainFrame.setMenuBar (mb); / / Assign the initial string is empty tempString = ""; / / Add text area mainFrame.add (ta, BorderLayout.CENTER); mainFrame.setSize (800, 500); mainFrame.setLocation (100, 100) ;/ / start position mainFrame.setResizable (true) ;/ / can not change the size mainFrame.setVisible (true); / / mainFrame.pack (); / / / / / / / / / / / / / / / / / / / / / / / / / Increase the monitor / / / / / / / / / / / / / / / / / / / / / / / / Main frame mainFrame.addWindowListener (new WindowAdapter () {/ / Close the window public void windowClosing (WindowEvent e) { System.exit (0); } } ) ; / / Text area ta.addKeyListener (new KeyAdapter () { public void KeyTyped (KeyEvent e) { textValueChanged = true; / / keyboard button that causes the text to modify } } ) ; / / / / / / / / / / / / / / / / " File" menu :/ / / / / / / / / / / / / / / / / / / / / / / / New miNew.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { ta.replaceRange ("", 0, ta.getText (). length ()) content ;/ / empty text area fileName = "" ;/ / file name cleared } } ) ; / / Open miOpen.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { FileDialog d = new FileDialog (mainFrame, "open file", FileDialog.LOAD) ;/ / open file dialog d.addWindowListener (new WindowAdapter () {/ / Close the file dialog window public void windowClosing (WindowEvent ee) { System.exit (0); } } ) ; d.setVisible (true); File f = new File (d.getDirectory () + d.getFile ()); / / Create a new document fileName = d.getDirectory () + d.getFile () ;/ / get the file name char ch [] = new char [(int) f.length ()] ;/ / / This file is used to establish the length of an array of characters try / / exception handling { / / Read and stored in the character array ch BufferedReader bw = new BufferedReader (new FileReader (f)); bw.read (ch); bw.close (); } catch (FileNotFoundException fe) { System.out.println ("file not found"); System.exit (0); } catch (IOException ie) { System.out.println ("IO error"); System.exit (0); } String s = new String (ch); ta.setText (s) ;/ / set the text area to open the file contents } } ) ; / / Save miSave.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { if (fileName.equals ("")) {/ / If the file is not saved , that file name is empty FileDialog d = new FileDialog (mainFrame, "save file", FileDialog.SAVE) ;/ / save file dialog d.addWindowListener (new WindowAdapter () {/ / Close the file dialog window public void windowClosing (WindowEvent ee) { System.exit (0); } } ) ; d.setVisible (true); String s = ta.getText () ;/ / get the input text try / / exception handling { File f = new File (d.getDirectory () + d.getFile ()) ;/ / New File fileName = d.getDirectory () + d.getFile () ;/ / get the file name BufferedWriter bw = new BufferedWriter (new FileWriter (f)) ;/ / input to a file bw.write (s, 0, s.length ()); bw.close (); } catch (FileNotFoundException fe_) { System.out.println ("file not found"); System.exit (0); } catch (IOException ie_) { System.out.println ("IO error"); System.exit (0); } } else / / If the file has been saved { String s = ta.getText () ;/ / get the input text try / / exception handling { File f = new File (fileName) ;/ / New File BufferedWriter bw = new BufferedWriter (new FileWriter (f)) ;/ / input to a file bw.write (s, 0, s.length ()); bw.close (); } catch (FileNotFoundException fe_) { System.out.println ("file not found"); System.exit (0); } catch (IOException ie_) { System.out.println ("IO error"); System.exit (0); } } } } ) ; / / Save as miSaveAs.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { FileDialog d = new FileDialog (mainFrame, "save file", FileDialog.SAVE) ;/ / save file dialog d.addWindowListener (new WindowAdapter () {/ / Close the file dialog window public void windowClosing (WindowEvent ee) { System.exit (0); } } ) ; d.setVisible (true); String s = ta.getText () ;/ / get the input text try / / exception handling { File f = new File (d.getDirectory () + d.getFile ()) ;/ / New File BufferedWriter bw = new BufferedWriter (new FileWriter (f)) ;/ / input to a file bw.write (s, 0, s.length ()); bw.close (); } catch (FileNotFoundException fe_) { System.out.println ("file not found"); System.exit (0); } catch (IOException ie_) { System.out.println ("IO error"); System.exit (0); } } } ) ; / / Exit miExit.addActionListener (new ActionListener () {/ / / exit the program public void actionPerformed (ActionEvent e) { System.exit (0); } } ) ; / / / / / / / / / / / / / / / / " Edit" menu :/ / / / / / / / / / / / / / / / / / / / / / Cut miCut.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { tempString = ta.getSelectedText (); / / / get the content you want to copy , temporary storage in the tempString StringBuffer tmp = new StringBuffer (ta.getText ()) ;/ / temporary storage of text int start = ta.getSelectionStart (); starting position / / get the string to be deleted int len = ta.getSelectedText () length ();. / / get the length of the string to be deleted tmp.delete (start, start + len); / / / delete the selected string ta.setText (tmp.toString ()) ;/ / set the original text with new text } } ) ; / / Copy miCopy.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { tempString = ta.getSelectedText (); / / / get the content you want to copy , temporary storage in the tempString } } ) ; / / Paste miPaste.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { StringBuffer tmp = new StringBuffer (ta.getText ()) ;/ / temporary storage of text int start = ta.getSelectionStart (); / / get the position you want to paste tmp.insert (start, tempString) ;/ / check the contents to be pasted into the ta.setText (tmp.toString ()) ;/ / set the original text with new text } } ) ; / / Delete miDelete.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { StringBuffer tmp = new StringBuffer (ta.getText ()) ;/ / temporary storage of text int start = ta.getSelectionStart (); starting position / / get the string to be deleted int len = ta.getSelectedText () length ();. / / get the length of the string to be deleted tmp.delete (start, start + len); / / / delete the selected string ta.setText (tmp.toString ()) ;/ / set the original text with new text } } ) ; / / / / / / / / / / / / / / / / " Format" menu :/ / / / / / / / / / / / / / / / / / / / / / Fonts miFont.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { final Dialog d = new Dialog (mainFrame, "Font") ;/ / New dialog box d.setLocation (250, 250) ;/ / start position d.setLayout (new BorderLayout ()) ;/ / table layout / / / / / / / / / / / / / / / / / / / / / / / / / / Upper part of the panel Label l_font = new Label ("font") ;/ / font tag Panel p_1 = new Panel (); p_1.add (l_font); p_1.setVisible (true); / / / / / / / / / / / / / / / / / / / / / / / / / / Part of the panel List font_list = new List (6, false) ;/ / font list / / Add font project font_list.add ("Plain") ;/ / / normal font font_list.add ("Bold"); / / / Bold font_list.add ("Italic") ;/ / italic font_list.addItemListener (new MyItemListener_font ()); / / font increase Monitors Panel p_2 = new Panel (); p_2.add (font_list); p_2.setVisible (true); / / / / / / / / / / / / / / / / / / / / / / / / / / Lower part of the panel Button ok = new Button ("OK"); ok.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { d.dispose (); } } ) ; ok.setSize (new Dimension (20, 5)); Panel p_3 = new Panel () ;/ / lower part of the panel p_3.add (ok); p_3.setVisible (true); / / Add three panels d.add (p_1, BorderLayout.NORTH); d.add (p_2, BorderLayout.CENTER); d.add (p_3, BorderLayout.SOUTH); d.pack (); d.addWindowListener (new WindowAdapter () {/ / Close the dialog window public void windowClosing (WindowEvent ee) { d.dispose (); } } ) ; d.setVisible (true); } } ) ; / / Turn uppercase lowercase letters miLowtoCapital.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { String s = ta.getText () ;/ / get the input text StringBuffer temp = new StringBuffer (""); for (int i = 0; i <s.length (); i + +) { if ((int) s.charAt (i)> = 97 && (int) s.charAt (i) <= 122) { temp.append ((char) ((int) s.charAt (i) -32)); } else temp.append (s.charAt (i)); } s = new String (temp); ta.setText (s); } } ) ; / / Turn uppercase lowercase miCapitaltoLow.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { String s = ta.getText () ;/ / get the input text StringBuffer temp = new StringBuffer (""); for (int i = 0; i <s.length (); i + +) { if ((int) s.charAt (i)> = 65 && (int) s.charAt (i) <= 90) { temp.append ((char) ((int) s.charAt (i) +32)); } else temp.append (s.charAt (i)); } s = new String (temp); ta.setText (s); } } ) ; / / Encryption miEncrypt.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { String s = ta.getText () ;/ / get the input text StringBuffer temp = new StringBuffer (""); for (int i = 0; i <s.length (); i + +) { if (s.charAt (i)> = 40 && s.charAt (i) <= 125) { if (i% 2 == 0) { temp.append ((char) (s.charAt (i) + 1)); } else temp.append ((char) (s.charAt (i) - 1)); } else temp.append (s.charAt (i)); } s = new String (temp); ta.setText (s); } } ) ; / / Decryption miDisencrypt.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { String s = ta.getText () ;/ / get the input text StringBuffer temp = new StringBuffer (""); for (int i = 0; i <s.length (); i + +) { if (s.charAt (i)> = 40 && s.charAt (i) <= 125) { if (i% 2 == 0) { temp.append ((char) (s.charAt (i) - 1)); } else temp.append ((char) (s.charAt (i) + 1)); } else temp.append (s.charAt (i)); } s = new String (temp); ta.setText (s); } } ) ; / / / / / / / / / / / / / / / / " Help" menu :/ / / / / / / / / / / / / / / / / / / / / / About notepad miAboutNotepad.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { final Dialog d = new Dialog (mainFrame, "AboutNotepad") ;/ / New dialog box TextArea t = new TextArea ("\ nwelcome to use Notepad" + "\ n \ n" + "Copyright @ Launching" + "\ n \ n" + "free software" + "\ n \ n" + "v0.99 " ) ;/ / add tags t.setSize (new Dimension (5, 5)); t.setEditable (false); d.setResizable (false) ;/ / non-resizable d.add (t); d.pack (); d.addWindowListener (new WindowAdapter () {/ / Close the dialog window public void windowClosing (WindowEvent ee) { d.dispose (); } } ) ; d.setLocation (100, 250) ;/ / start position d.setVisible (true); } } ) ; } class MyItemListener_font implements ItemListener {/ / font Listener public void itemStateChanged (ItemEvent e) { id_font = ((java.awt.List) e.getSource ()) getSelectedIndex ().; switch (id_font) { case 0: { ta.setFont (new Font ("Times New Roman", Font.PLAIN, ta.getFont (). getSize ())) ;/ / normal text break; } case 1: { ta.setFont (new Font ("Times New Roman", Font.BOLD, ta.getFont (). getSize ())) ;/ / bold text break; } case 2: { ta.setFont (new Font ("Times New Roman", Font.ITALIC, ta.getFont (). getSize ())) ;/ / italic text break; } } } } / / / / / / / / / / / / / / / / / / / / / / Main function / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / public static void main (String arg []) { Notepad test = new Notepad (); / / / Create a notepad } / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / }
[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.