1 19 20 package examples.texteditor; 21 22 import java.awt.FileDialog ; 23 import java.io.*; 24 25 public class Ted extends javax.swing.JFrame { 26 27 28 public Ted() { 29 initComponents (); 30 setSize(500,300); 31 } 32 33 38 private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane (); 40 textBox = new javax.swing.JTextArea (); 41 jMenuBar1 = new javax.swing.JMenuBar (); 42 jMenu1 = new javax.swing.JMenu (); 43 jMenuItem1 = new javax.swing.JMenuItem (); 44 jMenuItem4 = new javax.swing.JMenuItem (); 45 jMenuItem5 = new javax.swing.JMenuItem (); 46 jMenuItem6 = new javax.swing.JMenuItem (); 47 jMenuItem7 = new javax.swing.JMenuItem (); 48 jMenu2 = new javax.swing.JMenu (); 49 jMenuItem2 = new javax.swing.JMenuItem (); 50 jMenu3 = new javax.swing.JMenu (); 51 jMenuItem3 = new javax.swing.JMenuItem (); 52 53 setTitle("Ted"); 54 addWindowListener(new java.awt.event.WindowAdapter () { 55 public void windowClosing(java.awt.event.WindowEvent evt) { 56 exitForm(evt); 57 } 58 }); 59 60 jScrollPane1.setViewportView(textBox); 61 62 getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER); 63 64 jMenu1.setText("File"); 65 jMenuItem1.setText("New"); 66 jMenuItem1.addActionListener(new java.awt.event.ActionListener () { 67 public void actionPerformed(java.awt.event.ActionEvent evt) { 68 jMenuItem1ActionPerformed(evt); 69 } 70 }); 71 72 jMenu1.add(jMenuItem1); 73 jMenuItem4.setText("Open ..."); 74 jMenuItem4.addActionListener(new java.awt.event.ActionListener () { 75 public void actionPerformed(java.awt.event.ActionEvent evt) { 76 jMenuItem4ActionPerformed(evt); 77 } 78 }); 79 80 jMenu1.add(jMenuItem4); 81 jMenuItem5.setText("Save"); 82 jMenuItem5.addActionListener(new java.awt.event.ActionListener () { 83 public void actionPerformed(java.awt.event.ActionEvent evt) { 84 jMenuItem5ActionPerformed(evt); 85 } 86 }); 87 88 jMenu1.add(jMenuItem5); 89 jMenuItem6.setText("Save As ..."); 90 jMenuItem6.addActionListener(new java.awt.event.ActionListener () { 91 public void actionPerformed(java.awt.event.ActionEvent evt) { 92 jMenuItem6ActionPerformed(evt); 93 } 94 }); 95 96 jMenu1.add(jMenuItem6); 97 jMenuItem7.setText("Exit"); 98 jMenuItem7.addActionListener(new java.awt.event.ActionListener () { 99 public void actionPerformed(java.awt.event.ActionEvent evt) { 100 jMenuItem7ActionPerformed(evt); 101 } 102 }); 103 104 jMenu1.add(jMenuItem7); 105 jMenuBar1.add(jMenu1); 106 jMenu2.setText("Edit"); 107 jMenuItem2.setText("Find ..."); 108 jMenuItem2.addActionListener(new java.awt.event.ActionListener () { 109 public void actionPerformed(java.awt.event.ActionEvent evt) { 110 jMenuItem2ActionPerformed(evt); 111 } 112 }); 113 114 jMenu2.add(jMenuItem2); 115 jMenuBar1.add(jMenu2); 116 jMenu3.setText("Help"); 117 jMenuItem3.setText("About ..."); 118 jMenuItem3.addActionListener(new java.awt.event.ActionListener () { 119 public void actionPerformed(java.awt.event.ActionEvent evt) { 120 jMenuItem3ActionPerformed(evt); 121 } 122 }); 123 124 jMenu3.add(jMenuItem3); 125 jMenuBar1.add(jMenu3); 126 setJMenuBar(jMenuBar1); 127 128 } 130 private void jMenuItem5ActionPerformed (java.awt.event.ActionEvent evt) { if ("".equals(fileName)) 133 doSaveAs(); 134 else 135 doSave(fileName); 136 } 138 private void jMenuItem7ActionPerformed (java.awt.event.ActionEvent evt) { System.exit(0); 141 } 143 private void jMenuItem2ActionPerformed (java.awt.event.ActionEvent evt) { new Finder (this, textBox).show(); 146 } 148 private void jMenuItem3ActionPerformed (java.awt.event.ActionEvent evt) { new About(this). show(); 151 } 153 private void jMenuItem6ActionPerformed (java.awt.event.ActionEvent evt) { doSaveAs(); 156 } 158 private void jMenuItem4ActionPerformed (java.awt.event.ActionEvent evt) { FileDialog fileDialog = new FileDialog (this, "Open...", FileDialog.LOAD); 161 fileDialog.show (); 162 if (fileDialog.getFile () == null) 163 return; 164 fileName = fileDialog.getDirectory () + File.separator + fileDialog.getFile (); 165 166 FileInputStream fis = null; 167 String str = null; 168 try { 169 fis = new FileInputStream (fileName); 170 int size = fis.available (); 171 byte[] bytes = new byte [size]; 172 fis.read (bytes); 173 str = new String (bytes); 174 } catch (IOException e) { 175 } finally { 176 try { 177 fis.close (); 178 } catch (IOException e2) { 179 } 180 } 181 182 if (str != null) 183 textBox.setText (str); 184 } 186 private void jMenuItem1ActionPerformed (java.awt.event.ActionEvent evt) { fileName = ""; 189 textBox.setText (""); 190 } 192 193 private void exitForm(java.awt.event.WindowEvent evt) { System.exit (0); 195 } 197 private void doSave (String fileName) { 198 FileOutputStream fos = null; 199 String str = textBox.getText (); 200 try { 201 fos = new FileOutputStream (fileName); 202 fos.write (str.getBytes ()); 203 } catch (IOException e) { 204 } finally { 205 try { 206 fos.close (); 207 } catch (IOException e2) { 208 } 209 } 210 } 211 212 private void doSaveAs () { 213 FileDialog fileDialog = new FileDialog (this, "Save As...", FileDialog.SAVE); 214 fileDialog.show (); 215 if (fileDialog.getFile () == null) 216 return; 217 fileName = fileDialog.getDirectory () + File.separator + fileDialog.getFile (); 218 219 doSave (fileName); 220 } 221 222 private javax.swing.JMenuItem jMenuItem7; 224 private javax.swing.JMenuItem jMenuItem6; 225 private javax.swing.JMenuItem jMenuItem5; 226 private javax.swing.JMenuItem jMenuItem4; 227 private javax.swing.JMenuItem jMenuItem3; 228 private javax.swing.JMenuItem jMenuItem2; 229 private javax.swing.JMenuItem jMenuItem1; 230 private javax.swing.JScrollPane jScrollPane1; 231 private javax.swing.JTextArea textBox; 232 private javax.swing.JMenu jMenu3; 233 private javax.swing.JMenu jMenu2; 234 private javax.swing.JMenuBar jMenuBar1; 235 private javax.swing.JMenu jMenu1; 236 238 239 public static void main(java.lang.String [] args) { 240 new Ted ().show (); 241 } 242 243 private String fileName = ""; 244 } 245 | Popular Tags |