记事本

Posted mingye

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记事本相关的知识,希望对你有一定的参考价值。

   1 package mingye;
   2  import java.awt.*;  
   3  import java.awt.event.*;  
   4  import java.text.*;  
   5  import java.util.*;  
   6  import java.io.*;  
   7  import javax.swing.undo.*;  
   8  import javax.swing.border.*;  
   9  import javax.swing.*;  
  10  import javax.swing.text.*;  
  11  import javax.swing.event.*;  
  12  import java.awt.datatransfer.*;  
  13   
  14   
  15 public class NoteTest extends JFrame implements ActionListener,DocumentListener  
  16 {    
  17     JMenu fileMenu,editMenu,formatMenu,viewMenu,helpMenu;   
  18     JPopupMenu popupMenu;  
  19     JMenuItem popupMenu_Undo,popupMenu_Cut,popupMenu_Copy,popupMenu_Paste,popupMenu_Delete,popupMenu_SelectAll;  
  20     JMenuItem fileMenu_New,fileMenu_Open,fileMenu_Save,fileMenu_SaveAs,fileMenu_PageSetUp,fileMenu_Print,fileMenu_Exit;  
  21     JMenuItem editMenu_Undo,editMenu_Cut,editMenu_Copy,editMenu_Paste,editMenu_Delete,editMenu_Find,editMenu_FindNext,editMenu_Replace,editMenu_GoTo,editMenu_SelectAll,editMenu_TimeDate;   
  22     JCheckBoxMenuItem formatMenu_LineWrap;  
  23     JMenuItem formatMenu_Font;  
  24     JCheckBoxMenuItem viewMenu_Status;  
  25     JMenuItem helpMenu_HelpTopics,helpMenu_AboutNotepad;   
  26     JTextArea editArea;    
  27     JLabel statusLabel;    
  28     Toolkit toolkit=Toolkit.getDefaultToolkit();  
  29     Clipboard clipBoard=toolkit.getSystemClipboard();      
  30     protected UndoManager undo=new UndoManager();  
  31     protected UndoableEditListener undoHandler=new UndoHandler();    
  32     String oldValue;  
  33     boolean isNewFile=true;
  34     File currentFile;  
  35     public NoteTest()  
  36     {     
  37         super("Java记事本");  
  38          
  39         Font font = new Font("Dialog", Font.PLAIN, 12);  
  40         java.util.Enumeration keys = UIManager.getDefaults().keys();  
  41         while (keys.hasMoreElements()) {  
  42             Object key = keys.nextElement();  
  43             Object value = UIManager.get(key);  
  44             if (value instanceof javax.swing.plaf.FontUIResource) {  
  45                 UIManager.put(key, font);  
  46             }  
  47         }  
  48          
  49         JMenuBar menuBar=new JMenuBar();  
  50          
  51         fileMenu=new JMenu("文件(F)");  
  52         fileMenu.setMnemonic(‘F‘); 
  53   
  54         fileMenu_New=new JMenuItem("新建(N)");  
  55         fileMenu_New.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));  
  56         fileMenu_New.addActionListener(this);  
  57   
  58         fileMenu_Open=new JMenuItem("打开(O)...");  
  59         fileMenu_Open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));  
  60         fileMenu_Open.addActionListener(this);  
  61   
  62         fileMenu_Save=new JMenuItem("保存(S)");  
  63         fileMenu_Save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));  
  64         fileMenu_Save.addActionListener(this);  
  65   
  66         fileMenu_SaveAs=new JMenuItem("另存为(A)...");  
  67         fileMenu_SaveAs.addActionListener(this);  
  68   
  69         fileMenu_PageSetUp=new JMenuItem("页面设置(U)...");  
  70         fileMenu_PageSetUp.addActionListener(this);  
  71   
  72         fileMenu_Print=new JMenuItem("打印(P)...");  
  73         fileMenu_Print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK));   
  74         fileMenu_Print.addActionListener(this);  
  75   
  76         fileMenu_Exit=new JMenuItem("退出(X)");  
  77         fileMenu_Exit.addActionListener(this);  
  78   
  79         
  80         editMenu=new JMenu("编辑(E)");  
  81         editMenu.setMnemonic(‘E‘);
  82         editMenu.addMenuListener(new MenuListener()  
  83         {   public void menuCanceled(MenuEvent e)
  84             {   checkMenuItemEnabled();
  85             }  
  86             public void menuDeselected(MenuEvent e)
  87             {   checkMenuItemEnabled();
  88             }  
  89             public void menuSelected(MenuEvent e)
  90             {   checkMenuItemEnabled();
  91             }  
  92         });  
  93         try {
  94             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  95         } catch (ClassNotFoundException e1) {
  96             // TODO Auto-generated catch block
  97             e1.printStackTrace();
  98         } catch (InstantiationException e1) {
  99             // TODO Auto-generated catch block
 100             e1.printStackTrace();
 101         } catch (IllegalAccessException e1) {
 102             // TODO Auto-generated catch block
 103             e1.printStackTrace();
 104         } catch (UnsupportedLookAndFeelException e1) {
 105             // TODO Auto-generated catch block
 106             e1.printStackTrace();
 107         }
 108         editMenu_Undo=new JMenuItem("撤销(U)");  
 109         editMenu_Undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK));  
 110         editMenu_Undo.addActionListener(this);  
 111         editMenu_Undo.setEnabled(false);  
 112   
 113         editMenu_Cut=new JMenuItem("剪切(T)");  
 114         editMenu_Cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));  
 115         editMenu_Cut.addActionListener(this);  
 116   
 117         editMenu_Copy=new JMenuItem("复制(C)");  
 118         editMenu_Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));  
 119         editMenu_Copy.addActionListener(this);  
 120   
 121         editMenu_Paste=new JMenuItem("粘贴(P)");  
 122         editMenu_Paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));  
 123         editMenu_Paste.addActionListener(this);  
 124   
 125         editMenu_Delete=new JMenuItem("删除(D)");  
 126         editMenu_Delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));  
 127         editMenu_Delete.addActionListener(this);  
 128   
 129         editMenu_Find=new JMenuItem("查找(F)...");  
 130         editMenu_Find.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));  
 131         editMenu_Find.addActionListener(this);  
 132   
 133         editMenu_FindNext=new JMenuItem("查找下一个(N)");  
 134         editMenu_FindNext.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,0));  
 135         editMenu_FindNext.addActionListener(this);  
 136   
 137         editMenu_Replace = new JMenuItem("替换(R)...",‘R‘);   
 138         editMenu_Replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK));   
 139         editMenu_Replace.addActionListener(this);  
 140   
 141         editMenu_GoTo = new JMenuItem("转到(G)...",‘G‘);   
 142         editMenu_GoTo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_MASK));   
 143         editMenu_GoTo.addActionListener(this);  
 144   
 145         editMenu_SelectAll = new JMenuItem("全选",‘A‘);   
 146         editMenu_SelectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));   
 147         editMenu_SelectAll.addActionListener(this);  
 148   
 149         editMenu_TimeDate = new JMenuItem("时间/日期(D)",‘D‘);  
 150         editMenu_TimeDate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0));  
 151         editMenu_TimeDate.addActionListener(this);  
 152   
 153           
 154         formatMenu=new JMenu("格式(O)");  
 155         formatMenu.setMnemonic(‘O‘);
 156   
 157         formatMenu_LineWrap=new JCheckBoxMenuItem("自动换行(W)");  
 158         formatMenu_LineWrap.setMnemonic(‘W‘); 
 159         formatMenu_LineWrap.setState(true);  
 160         formatMenu_LineWrap.addActionListener(this);  
 161         formatMenu_Font=new JMenuItem("字体(F)...");  
 162         formatMenu_Font.addActionListener(this);  
 163         viewMenu=new JMenu("查看(V)");  
 164         viewMenu.setMnemonic(‘V‘);
 165         viewMenu_Status=new JCheckBoxMenuItem("状态栏(S)");  
 166         viewMenu_Status.setMnemonic(‘S‘); 
 167         viewMenu_Status.setState(true);  
 168         viewMenu_Status.addActionListener(this);    
 169         helpMenu = new JMenu("帮助(H)");  
 170         helpMenu.setMnemonic(‘H‘);  
 171         helpMenu_AboutNotepad = new JMenuItem("关于记事本(A)");   
 172         helpMenu_AboutNotepad.addActionListener(this);  
 173     
 174         menuBar.add(fileMenu);   
 175         fileMenu.add(fileMenu_New);   
 176         fileMenu.add(fileMenu_Open);   
 177         fileMenu.add(fileMenu_Save);   
 178         fileMenu.add(fileMenu_SaveAs);   
 179         fileMenu.addSeparator();         
 180         fileMenu.add(fileMenu_PageSetUp);   
 181         fileMenu.add(fileMenu_Print);  
 182         fileMenu.addSeparator();          
 183         fileMenu.add(fileMenu_Exit);   
 184   
 185         menuBar.add(editMenu);   
 186         editMenu.add(editMenu_Undo);    
 187         editMenu.addSeparator();           
 188         editMenu.add(editMenu_Cut);   
 189         editMenu.add(editMenu_Copy);   
 190         editMenu.add(editMenu_Paste);   
 191         editMenu.add(editMenu_Delete);   
 192         editMenu.addSeparator();        
 193         editMenu.add(editMenu_Find);   
 194         editMenu.add(editMenu_FindNext);   
 195         editMenu.add(editMenu_Replace);  
 196         editMenu.add(editMenu_GoTo);   
 197         editMenu.addSeparator();        
 198         editMenu.add(editMenu_SelectAll);   
 199         editMenu.add(editMenu_TimeDate);  
 200   
 201               
 202         menuBar.add(formatMenu);   
 203         formatMenu.add(formatMenu_LineWrap);   
 204         formatMenu.add(formatMenu_Font);  
 205   
 206         menuBar.add(viewMenu);   
 207         viewMenu.add(viewMenu_Status);  
 208   
 209         menuBar.add(helpMenu);  
 210        
 211        
 212         helpMenu.add(helpMenu_AboutNotepad);  
 213                        
 214         this.setJMenuBar(menuBar);  
 215    
 216         editArea=new JTextArea(20,50);  
 217         JScrollPane scroller=new JScrollPane(editArea);  
 218         scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);  
 219         this.add(scroller,BorderLayout.CENTER);
 220         editArea.setWrapStyleWord(true); 
 221         editArea.setLineWrap(true);
 222         oldValue=editArea.getText();
 223         editArea.getDocument().addUndoableEditListener(undoHandler);  
 224         editArea.getDocument().addDocumentListener(this);  
 225         popupMenu=new JPopupMenu();  
 226         popupMenu_Undo=new JMenuItem("撤销(U)");  
 227         popupMenu_Cut=new JMenuItem("剪切(T)");  
 228         popupMenu_Copy=new JMenuItem("复制(C)");  
 229         popupMenu_Paste=new JMenuItem("粘帖(P)");  
 230         popupMenu_Delete=new JMenuItem("删除(D)");  
 231         popupMenu_SelectAll=new JMenuItem("全选(A)");  
 232         popupMenu_Undo.setEnabled(false);  
 233         popupMenu.add(popupMenu_Undo);  
 234         popupMenu.addSeparator();  
 235         popupMenu.add(popupMenu_Cut);  
 236         popupMenu.add(popupMenu_Copy);  
 237         popupMenu.add(popupMenu_Paste);  
 238         popupMenu.add(popupMenu_Delete);  
 239         popupMenu.addSeparator();  
 240         popupMenu.add(popupMenu_SelectAll);  
 241         popupMenu_Undo.addActionListener(this);  
 242         popupMenu_Cut.addActionListener(this);  
 243         popupMenu_Copy.addActionListener(this);  
 244         popupMenu_Paste.addActionListener(this);  
 245         popupMenu_Delete.addActionListener(this);  
 246         popupMenu_SelectAll.addActionListener(this);  
 247         editArea.addMouseListener(new MouseAdapter()  
 248         {   public void mousePressed(MouseEvent e)  
 249             {   if(e.isPopupTrigger())
 250                 {   popupMenu.show(e.getComponent(),e.getX(),e.getY());
 251                 }  
 252                 checkMenuItemEnabled();
 253                 editArea.requestFocus();
 254             }  
 255             public void mouseReleased(MouseEvent e)  
 256             {   if(e.isPopupTrigger())
 257                 {   popupMenu.show(e.getComponent(),e.getX(),e.getY());
 258                 }  
 259                 checkMenuItemEnabled();
 260                 editArea.requestFocus();
 261             }  
 262         });
 263    
 264 
 265        setBounds(360,100,650,500);
 266        setVisible(true);  
 267        
 268         addWindowListener(new WindowAdapter()  
 269         {   public void windowClosing(WindowEvent e)  
 270             {   exitWindowChoose();  
 271             }  
 272         });  
 273   
 274         checkMenuItemEnabled();  
 275         editArea.requestFocus();  
 276     }
 277    
 278     public void checkMenuItemEnabled()  
 279     {   String selectText=editArea.getSelectedText();  
 280         if(selectText==null)  
 281         {   editMenu_Cut.setEnabled(false);  
 282             popupMenu_Cut.setEnabled(false);  
 283             editMenu_Copy.setEnabled(false);  
 284             popupMenu_Copy.setEnabled(false);  
 285             editMenu_Delete.setEnabled(false);  
 286             popupMenu_Delete.setEnabled(false);  
 287         }  
 288         else  
 289         {   editMenu_Cut.setEnabled(true);  
 290             popupMenu_Cut.setEnabled(true);   
 291             editMenu_Copy.setEnabled(true);  
 292             popupMenu_Copy.setEnabled(true);  
 293             editMenu_Delete.setEnabled(true);  
 294             popupMenu_Delete.setEnabled(true);  
 295         }  
 296        
 297         Transferable contents=clipBoard.getContents(this);  
 298         if(contents==null)  
 299         {   editMenu_Paste.setEnabled(false);  
 300             popupMenu_Paste.setEnabled(false);  
 301         }  
 302         else  
 303         {   editMenu_Paste.setEnabled(true);  
 304             popupMenu_Paste.setEnabled(true);     
 305         }  
 306     }
 307    
 308     public void exitWindowChoose()  
 309     {   editArea.requestFocus();  
 310         String currentValue=editArea.getText();  
 311         if(currentValue.equals(oldValue)==true)  
 312         {   System.exit(0);  
 313         }  
 314         else  
 315         {   int exitChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存,是否保存?","退出提示",JOptionPane.YES_NO_CANCEL_OPTION);  
 316             if(exitChoose==JOptionPane.YES_OPTION)  
 317             {    
 318                 if(isNewFile)  
 319                 {     
 320                     String str=null;  
 321                     JFileChooser fileChooser=new JFileChooser();  
 322                     fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
 323                     fileChooser.setApproveButtonText("确定");  
 324                     fileChooser.setDialogTitle("另存为");  
 325                       
 326                     int result=fileChooser.showSaveDialog(this);  
 327                       
 328                     if(result==JFileChooser.CANCEL_OPTION)  
 329                     {   statusLabel.setText(" 您没有保存文件");  
 330                         return;  
 331                     }                     
 332       
 333                     File saveFileName=fileChooser.getSelectedFile();  
 334                   
 335                     if(saveFileName==null||saveFileName.getName().equals(""))  
 336                     {   JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);  
 337                     }  
 338                     else   
 339                     {   try  
 340                         {   FileWriter fw=new FileWriter(saveFileName);  
 341                             BufferedWriter bfw=new BufferedWriter(fw);  
 342                             bfw.write(editArea.getText(),0,editArea.getText().length());  
 343                             bfw.flush();  
 344                             fw.close();  
 345                               
 346                             isNewFile=false;  
 347                             currentFile=saveFileName;  
 348                             oldValue=editArea.getText();  
 349                               
 350                             this.setTitle(saveFileName.getName()+"  - 记事本");  
 351                             statusLabel.setText(" 当前打开文件:"+saveFileName.getAbsoluteFile());  
 352                             //isSave=true;  
 353                         }                             
 354                         catch(IOException ioException){                   
 355                         }                 
 356                     }  
 357                 }  
 358                 else  
 359                 {  
 360                     try  
 361                     {   FileWriter fw=new FileWriter(currentFile);  
 362                         BufferedWriter bfw=new BufferedWriter(fw);  
 363                         bfw.write(editArea.getText(),0,editArea.getText().length());  
 364                         bfw.flush();  
 365                         fw.close();  
 366                       
 367                     }                             
 368                     catch(IOException ioException){                   
 369                     }  
 370                 }  
 371                 System.exit(0);  
 372            
 373             }  
 374             else if(exitChoose==JOptionPane.NO_OPTION)  
 375             {   System.exit(0);  
 376             }  
 377             else  
 378             {   return;  
 379             }  
 380         }  
 381     } 
 382   
 383     public void find()  
 384     {   final JDialog findDialog=new JDialog(this,"查找",false);
 385         Container con=findDialog.getContentPane();    
 386         con.setLayout(new FlowLayout(FlowLayout.LEFT));  
 387         JLabel findContentLabel=new JLabel("查找内容(N):");  
 388         final JTextField findText=new JTextField(15);  
 389         JButton findNextButton=new JButton("查找下一个(F):");  
 390         final JCheckBox matchCheckBox=new JCheckBox("区分大小写(C)");  
 391         ButtonGroup bGroup=new ButtonGroup();  
 392         final JRadioButton upButton=new JRadioButton("向上(U)");  
 393         final JRadioButton downButton=new JRadioButton("向下(U)");  
 394         downButton.setSelected(true);  
 395         bGroup.add(upButton);  
 396         bGroup.add(downButton);  
 397         
 398         JButton cancel=new JButton("取消");  
 399         
 400         cancel.addActionListener(new ActionListener()  
 401         {   public void actionPerformed(ActionEvent e)  
 402             {   findDialog.dispose();  
 403             }  
 404         });  
 405  
 406         findNextButton.addActionListener(new ActionListener()  
 407         {   public void actionPerformed(ActionEvent e)  
 408             {  
 409                 int k=0,m=0;  
 410                 final String str1,str2,str3,str4,strA,strB;  
 411                 str1=editArea.getText();  
 412                 str2=findText.getText();  
 413                 str3=str1.toUpperCase();  
 414                 str4=str2.toUpperCase();  
 415                 if(matchCheckBox.isSelected())
 416                 {   strA=str1;  
 417                     strB=str2;  
 418                 }  
 419                 else
 420                 {   strA=str3;  
 421                     strB=str4;  
 422                 }  
 423                 if(upButton.isSelected())  
 424                 {  
 425                     if(editArea.getSelectedText()==null)  
 426                         k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);  
 427                     else  
 428                         k=strA.lastIndexOf(strB, editArea.getCaretPosition()-findText.getText().length()-1);      
 429                     if(k>-1)  
 430                     {   
 431                         editArea.setCaretPosition(k);  
 432                         editArea.select(k,k+strB.length());  
 433                     }  
 434                     else  
 435                     {   JOptionPane.showMessageDialog(null,"查找失败!","查找",JOptionPane.INFORMATION_MESSAGE);  
 436                     }  
 437                 }  
 438                 else if(downButton.isSelected())  
 439                 {   if(editArea.getSelectedText()==null)  
 440                         k=strA.indexOf(strB,editArea.getCaretPosition()+1);  
 441                     else  
 442                         k=strA.indexOf(strB, editArea.getCaretPosition()-findText.getText().length()+1);      
 443                     if(k>-1)  
 444                     {   //String strData=strA.subString(k,strB.getText().length()+1);  
 445                         editArea.setCaretPosition(k);  
 446                         editArea.select(k,k+strB.length());  
 447                     }  
 448                     else  
 449                     {   JOptionPane.showMessageDialog(null,"查找失败!","查找",JOptionPane.INFORMATION_MESSAGE);  
 450                     }  
 451                 }  
 452             }  
 453         });
 454         JPanel panel1=new JPanel();  
 455         JPanel panel2=new JPanel();  
 456         JPanel panel3=new JPanel();  
 457         JPanel directionPanel=new JPanel();  
 458         directionPanel.setBorder(BorderFactory.createTitledBorder("方向"));  
 459         directionPanel.add(upButton);  
 460         directionPanel.add(downButton);  
 461         panel1.setLayout(new GridLayout(2,1));  
 462         panel1.add(findNextButton);  
 463         panel1.add(cancel);  
 464         panel2.add(findContentLabel);  
 465         panel2.add(findText);  
 466         panel2.add(panel1);  
 467         panel3.add(matchCheckBox);  
 468         panel3.add(directionPanel);  
 469         con.add(panel2);  
 470         con.add(panel3);  
 471         findDialog.setSize(410,180);  
 472         findDialog.setResizable(false);//不可调整大小  
 473         findDialog.setLocation(230,280);  
 474         findDialog.setVisible(true);  
 475     }  
 476   
 477     public void replace()  
 478     {   final JDialog replaceDialog=new JDialog(this,"替换",false);
 479         Container con=replaceDialog.getContentPane();
 480         con.setLayout(new FlowLayout(FlowLayout.CENTER));  
 481         JLabel findContentLabel=new JLabel("查找内容(N):");  
 482         final JTextField findText=new JTextField(15);  
 483         JButton findNextButton=new JButton("查找下一个(F):");  
 484         JLabel replaceLabel=new JLabel("替换为(P):");  
 485         final JTextField replaceText=new JTextField(15);  
 486         JButton replaceButton=new JButton("替换(R)");  
 487         JButton replaceAllButton=new JButton("全部替换(A)");  
 488         JButton cancel=new JButton("取消");  
 489         cancel.addActionListener(new ActionListener()  
 490         {   public void actionPerformed(ActionEvent e)  
 491             {   replaceDialog.dispose();  
 492             }  
 493         });  
 494         final JCheckBox matchCheckBox=new JCheckBox("区分大小写(C)");  
 495         ButtonGroup bGroup=new ButtonGroup();  
 496         final JRadioButton upButton=new JRadioButton("向上(U)");  
 497         final JRadioButton downButton=new JRadioButton("向下(U)");  
 498         downButton.setSelected(true);  
 499         bGroup.add(upButton);  
 500       
 501         findNextButton.addActionListener(new ActionListener()  
 502         {   public void actionPerformed(ActionEvent e)  
 503             {   
 504                 int k=0,m=0;  
 505                 final String str1,str2,str3,str4,strA,strB;  
 506                 str1=editArea.getText();  
 507                 str2=findText.getText();  
 508                 str3=str1.toUpperCase();  
 509                 str4=str2.toUpperCase();  
 510                 if(matchCheckBox.isSelected())//区分大小写  
 511                 {   strA=str1;  
 512                     strB=str2;  
 513                 }  
 514                 else
 515                 {   strA=str3;  
 516                     strB=str4;  
 517                 }  
 518                 if(upButton.isSelected())  
 519                 {   
 520                     if(editArea.getSelectedText()==null)  
 521                         k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);  
 522                     else  
 523                         k=strA.lastIndexOf(strB, editArea.getCaretPosition()-findText.getText().length()-1);      
 524                     if(k>-1)  
 525                     {    
 526                         editArea.setCaretPosition(k);  
 527                         editArea.select(k,k+strB.length());  
 528                     }  
 529                     else  
 530                     {   JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);  
 531                     }  
 532                 }  
 533                 else if(downButton.isSelected())  
 534                 {   if(editArea.getSelectedText()==null)  
 535                         k=strA.indexOf(strB,editArea.getCaretPosition()+1);  
 536                     else  
 537                         k=strA.indexOf(strB, editArea.getCaretPosition()-findText.getText().length()+1);      
 538                     if(k>-1)  
 539                     {    
 540                         editArea.setCaretPosition(k);  
 541                         editArea.select(k,k+strB.length());  
 542                     }  
 543                     else  
 544                     {   JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);  
 545                     }  
 546                 }  
 547             }  
 548         });
 549           
 550         
 551         replaceButton.addActionListener(new ActionListener()  
 552         {   public void actionPerformed(ActionEvent e)  
 553             {   if(replaceText.getText().length()==0 && editArea.getSelectedText()!=null)   
 554                     editArea.replaceSelection("");   
 555                 if(replaceText.getText().length()>0 && editArea.getSelectedText()!=null)   
 556                     editArea.replaceSelection(replaceText.getText());  
 557             }  
 558         });
 559          
 560         replaceAllButton.addActionListener(new ActionListener()  
 561         {   public void actionPerformed(ActionEvent e)  
 562             {   editArea.setCaretPosition(0);      
 563                 int k=0,m=0,replaceCount=0;  
 564                 if(findText.getText().length()==0)  
 565                 {   JOptionPane.showMessageDialog(replaceDialog,"请填写您要查找的内容!","提示",JOptionPane.WARNING_MESSAGE);  
 566                     findText.requestFocus(true);  
 567                     return;  
 568                 }  
 569                 while(k>-1)
 570                 {   
 571                     final String str1,str2,str3,str4,strA,strB;  
 572                     str1=editArea.getText();  
 573                     str2=findText.getText();  
 574                     str3=str1.toUpperCase();  
 575                     str4=str2.toUpperCase();  
 576                     if(matchCheckBox.isSelected())//区分大小写  
 577                     {   strA=str1;  
 578                         strB=str2;  
 579                     }  
 580                     else 
 581                     {   strA=str3;  
 582                         strB=str4;  
 583                     }  
 584                     if(upButton.isSelected())  
 585                     {   
 586                         if(editArea.getSelectedText()==null)  
 587                             k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);  
 588                         else  
 589                             k=strA.lastIndexOf(strB, editArea.getCaretPosition()-findText.getText().length()-1);      
 590                         if(k>-1)  
 591                         {   
 592                             editArea.setCaretPosition(k);  
 593                             editArea.select(k,k+strB.length());  
 594                         }  
 595                         else  
 596                         {   if(replaceCount==0)  
 597                             {   JOptionPane.showMessageDialog(replaceDialog, "查找失败!", "记事本",JOptionPane.INFORMATION_MESSAGE);   
 598                             }  
 599                             else  
 600                             {   JOptionPane.showMessageDialog(replaceDialog,"成功替换"+replaceCount+"个相关内容!","替换成功",JOptionPane.INFORMATION_MESSAGE);  
 601                             }  
 602                         }  
 603                     }  
 604                     else if(downButton.isSelected())  
 605                     {   if(editArea.getSelectedText()==null)  
 606                             k=strA.indexOf(strB,editArea.getCaretPosition()+1);  
 607                         else  
 608                             k=strA.indexOf(strB, editArea.getCaretPosition()-findText.getText().length()+1);      
 609                         if(k>-1)  
 610                         { 
 611                             editArea.setCaretPosition(k);  
 612                             editArea.select(k,k+strB.length());  
 613                         }  
 614                         else  
 615                         {   if(replaceCount==0)  
 616                             {   JOptionPane.showMessageDialog(replaceDialog, "查找失败!", "记事本",JOptionPane.INFORMATION_MESSAGE);   
 617                             }  
 618                             else  
 619                             {   JOptionPane.showMessageDialog(replaceDialog,"成功替换"+replaceCount+"个匹配内容!","替换成功",JOptionPane.INFORMATION_MESSAGE);  
 620                             }  
 621                         }  
 622                     }  
 623                     if(replaceText.getText().length()==0 && editArea.getSelectedText()!= null)  
 624                     {   editArea.replaceSelection("");  
 625                         replaceCount++;  
 626                     }   
 627                       
 628                     if(replaceText.getText().length()>0 && editArea.getSelectedText()!= null)   
 629                     {   editArea.replaceSelection(replaceText.getText());   
 630                         replaceCount++;  
 631                     }  
 632                 }
 633             }  
 634         });    
 635         JPanel directionPanel=new JPanel();  
 636         directionPanel.setBorder(BorderFactory.createTitledBorder("方向"));  
 637         directionPanel.add(upButton);  
 638         directionPanel.add(downButton);  
 639         JPanel panel1=new JPanel();  
 640         JPanel panel2=new JPanel();  
 641         JPanel panel3=new JPanel();  
 642         JPanel panel4=new JPanel();  
 643         panel4.setLayout(new GridLayout(2,1));  
 644         panel1.add(findContentLabel);  
 645         panel1.add(findText);  
 646         panel1.add(findNextButton);  
 647         panel4.add(replaceButton);  
 648         panel4.add(replaceAllButton);  
 649         panel2.add(replaceLabel);  
 650         panel2.add(replaceText);  
 651         panel2.add(panel4);  
 652         panel3.add(matchCheckBox);  
 653         panel3.add(directionPanel);  
 654         panel3.add(cancel);  
 655         con.add(panel1);  
 656         con.add(panel2);  
 657         con.add(panel3);  
 658         replaceDialog.setSize(420,220);  
 659         replaceDialog.setResizable(false);//不可调整大小  
 660         replaceDialog.setLocation(230,280);  
 661         replaceDialog.setVisible(true);  
 662     }
 663     public void font()  
 664     {   final JDialog fontDialog=new JDialog(this,"字体设置",false);  
 665         Container con=fontDialog.getContentPane();  
 666         con.setLayout(new FlowLayout(FlowLayout.LEFT));  
 667         JLabel fontLabel=new JLabel("字体(F):");  
 668         fontLabel.setPreferredSize(new Dimension(100,20));
 669         JLabel styleLabel=new JLabel("字形(Y):");  
 670         styleLabel.setPreferredSize(new Dimension(100,20));  
 671         JLabel sizeLabel=new JLabel("大小(S):");  
 672         sizeLabel.setPreferredSize(new Dimension(100,20));  
 673         final JLabel sample=new JLabel("明少的记事本");  
 674         final JTextField fontText=new JTextField(9);  
 675         fontText.setPreferredSize(new Dimension(200,20));  
 676         final JTextField styleText=new JTextField(8);  
 677         styleText.setPreferredSize(new Dimension(200,20));  
 678         final int style[]={Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC};  
 679         final JTextField sizeText=new JTextField(5);  
 680         sizeText.setPreferredSize(new Dimension(200,20));  
 681         JButton okButton=new JButton("确定");  
 682         JButton cancel=new JButton("取消");  
 683         cancel.addActionListener(new ActionListener()  
 684         {   public void actionPerformed(ActionEvent e)  
 685             {   fontDialog.dispose();     
 686             }  
 687         });  
 688         Font currentFont=editArea.getFont();  
 689         fontText.setText(currentFont.getFontName());  
 690         fontText.selectAll();  
 691         if(currentFont.getStyle()==Font.PLAIN)  
 692             styleText.setText("常规");  
 693         else if(currentFont.getStyle()==Font.BOLD)  
 694             styleText.setText("粗体");  
 695         else if(currentFont.getStyle()==Font.ITALIC)  
 696             styleText.setText("斜体");  
 697         else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC))  
 698             styleText.setText("粗斜体");  
 699         styleText.selectAll();  
 700         String str=String.valueOf(currentFont.getSize());  
 701         sizeText.setText(str);  
 702         sizeText.selectAll();  
 703         final JList fontList,styleList,sizeList;  
 704         GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();  
 705         final String fontName[]=ge.getAvailableFontFamilyNames();  
 706         fontList=new JList(fontName);  
 707         fontList.setFixedCellWidth(86);  
 708         fontList.setFixedCellHeight(20);  
 709         fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
 710         final String fontStyle[]={"常规","粗体","斜体","粗斜体"};  
 711         styleList=new JList(fontStyle);  
 712         styleList.setFixedCellWidth(86);  
 713         styleList.setFixedCellHeight(20);  
 714         styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
 715         if(currentFont.getStyle()==Font.PLAIN)  
 716             styleList.setSelectedIndex(0);  
 717         else if(currentFont.getStyle()==Font.BOLD)  
 718             styleList.setSelectedIndex(1);  
 719         else if(currentFont.getStyle()==Font.ITALIC)  
 720             styleList.setSelectedIndex(2);  
 721         else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC))  
 722             styleList.setSelectedIndex(3);  
 723         final String fontSize[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};  
 724         sizeList=new JList(fontSize);  
 725         sizeList.setFixedCellWidth(43);  
 726         sizeList.setFixedCellHeight(20);  
 727         sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
 728         fontList.addListSelectionListener(new ListSelectionListener()  
 729         {   public void valueChanged(ListSelectionEvent event)  
 730             {   fontText.setText(fontName[fontList.getSelectedIndex()]);  
 731                 fontText.selectAll();  
 732                 Font sampleFont1=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText()));  
 733                 sample.setFont(sampleFont1);  
 734             }  
 735         });  
 736         styleList.addListSelectionListener(new ListSelectionListener()  
 737         {   public void valueChanged(ListSelectionEvent event)  
 738             {   int s=style[styleList.getSelectedIndex()];  
 739                 styleText.setText(fontStyle[s]);  
 740                 styleText.selectAll();  
 741                 Font sampleFont2=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText()));  
 742                 sample.setFont(sampleFont2);  
 743             }  
 744         });  
 745         sizeList.addListSelectionListener(new ListSelectionListener()  
 746         {   public void valueChanged(ListSelectionEvent event)  
 747             {   sizeText.setText(fontSize[sizeList.getSelectedIndex()]);  
 748                 //sizeText.requestFocus();  
 749                 sizeText.selectAll();     
 750                 Font sampleFont3=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText()));  
 751                 sample.setFont(sampleFont3);  
 752             }  
 753         });  
 754         okButton.addActionListener(new ActionListener()  
 755         {   public void actionPerformed(ActionEvent e)  
 756             {   Font okFont=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText()));  
 757                 editArea.setFont(okFont);  
 758                 fontDialog.dispose();  
 759             }  
 760         });  
 761         JPanel samplePanel=new JPanel();  
 762         samplePanel.setBorder(BorderFactory.createTitledBorder("示例"));  
 763         samplePanel.add(sample);  
 764         JPanel panel1=new JPanel();  
 765         JPanel panel2=new JPanel();  
 766         JPanel panel3=new JPanel();    
 767         panel2.add(fontText);  
 768         panel2.add(styleText);  
 769         panel2.add(sizeText);  
 770         panel2.add(okButton);  
 771         panel3.add(new JScrollPane(fontList));//JList不支持直接滚动,所以要让JList作为JScrollPane的视口视图  
 772         panel3.add(new JScrollPane(styleList));  
 773         panel3.add(new JScrollPane(sizeList));  
 774         panel3.add(cancel);  
 775         con.add(panel1);  
 776         con.add(panel2);  
 777         con.add(panel3);  
 778         con.add(samplePanel);  
 779         fontDialog.setSize(350,340);  
 780         fontDialog.setLocation(200,200);  
 781         fontDialog.setResizable(false);  
 782         fontDialog.setVisible(true);  
 783     }  
 784   
 785   
 786     public void actionPerformed(ActionEvent e)  
 787     {    
 788         if(e.getSource()==fileMenu_New)  
 789         {   editArea.requestFocus();  
 790             String currentValue=editArea.getText();  
 791             boolean isTextChange=(currentValue.equals(oldValue))?false:true;  
 792             if(isTextChange)  
 793             {   int saveChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);  
 794                 if(saveChoose==JOptionPane.YES_OPTION)  
 795                 {   String str=null;  
 796                     JFileChooser fileChooser=new JFileChooser();  
 797                     fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
 798                     fileChooser.setDialogTitle("另存为");  
 799                     int result=fileChooser.showSaveDialog(this);  
 800                     if(result==JFileChooser.CANCEL_OPTION)  
 801                     {   statusLabel.setText("您没有选择任何文件");  
 802                         return;  
 803                     }  
 804                     File saveFileName=fileChooser.getSelectedFile();  
 805                     if(saveFileName==null || saveFileName.getName().equals(""))  
 806                     {   JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);  
 807                     }  
 808                     else   
 809                     {   try  
 810                         {   FileWriter fw=new FileWriter(saveFileName);  
 811                             BufferedWriter bfw=new BufferedWriter(fw);  
 812                             bfw.write(editArea.getText(),0,editArea.getText().length());  
 813                             bfw.flush();  
 814                             bfw.close();  
 815                             isNewFile=false;  
 816                             currentFile=saveFileName;  
 817                             oldValue=editArea.getText();  
 818                             this.setTitle(saveFileName.getName()+" - 记事本");  
 819                             statusLabel.setText("当前打开文件:"+saveFileName.getAbsoluteFile());  
 820                         }  
 821                         catch (IOException ioException)  
 822                         {  
 823                         }  
 824                     }  
 825                 }  
 826                 else if(saveChoose==JOptionPane.NO_OPTION)  
 827                 {   editArea.replaceRange("",0,editArea.getText().length());  
 828                     statusLabel.setText(" 新建文件");  
 829                     this.setTitle("无标题 - 记事本");  
 830                     isNewFile=true;  
 831                     undo.discardAllEdits(); 
 832                     editMenu_Undo.setEnabled(false);  
 833                     oldValue=editArea.getText();  
 834                 }  
 835                 else if(saveChoose==JOptionPane.CANCEL_OPTION)  
 836                 {   return;  
 837                 }  
 838             }  
 839             else  
 840             {   editArea.replaceRange("",0,editArea.getText().length());  
 841                 statusLabel.setText(" 新建文件");  
 842                 this.setTitle("无标题 - 记事本");  
 843                 isNewFile=true;  
 844                 undo.discardAllEdits();
 845                 editMenu_Undo.setEnabled(false);  
 846                 oldValue=editArea.getText();  
 847             }  
 848         }
 849    
 850         else if(e.getSource()==fileMenu_Open)  
 851         {   editArea.requestFocus();  
 852             String currentValue=editArea.getText();  
 853             boolean isTextChange=(currentValue.equals(oldValue))?false:true;  
 854             if(isTextChange)  
 855             {   int saveChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);  
 856                 if(saveChoose==JOptionPane.YES_OPTION)  
 857                 {   String str=null;  
 858                     JFileChooser fileChooser=new JFileChooser();  
 859                     fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
 860                     fileChooser.setDialogTitle("另存为");  
 861                     int result=fileChooser.showSaveDialog(this);  
 862                     if(result==JFileChooser.CANCEL_OPTION)  
 863                     {   statusLabel.setText("您没有选择任何文件");  
 864                         return;  
 865                     }  
 866                     File saveFileName=fileChooser.getSelectedFile();  
 867                     if(saveFileName==null || saveFileName.getName().equals(""))  
 868                     {   JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);  
 869                     }  
 870                     else   
 871                     {   try  
 872                         {   FileWriter fw=new FileWriter(saveFileName);  
 873                             BufferedWriter bfw=new BufferedWriter(fw);  
 874                             bfw.write(editArea.getText(),0,editArea.getText().length());  
 875                             bfw.flush();
 876                             bfw.close();  
 877                             isNewFile=false;  
 878                             currentFile=saveFileName;  
 879                             oldValue=editArea.getText();  
 880                             this.setTitle(saveFileName.getName()+" - 记事本");  
 881                             statusLabel.setText("当前打开文件:"+saveFileName.getAbsoluteFile());  
 882                         }  
 883                         catch (IOException ioException)  
 884                         {  
 885                         }  
 886                     }  
 887                 }  
 888                 else if(saveChoose==JOptionPane.NO_OPTION)  
 889                 {   String str=null;  
 890                     JFileChooser fileChooser=new JFileChooser();  
 891                     fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
 892                   
 893                     fileChooser.setDialogTitle("打开文件");  
 894                     int result=fileChooser.showOpenDialog(this);  
 895                     if(result==JFileChooser.CANCEL_OPTION)  
 896                     {   statusLabel.setText("您没有选择任何文件");  
 897                         return;  
 898                     }  
 899                     File fileName=fileChooser.getSelectedFile();  
 900                     if(fileName==null || fileName.getName().equals(""))  
 901                     {   JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);  
 902                     }  
 903                     else  
 904                     {   try  
 905                         {   FileReader fr=new FileReader(fileName);  
 906                             BufferedReader bfr=new BufferedReader(fr);  
 907                             editArea.setText("");  
 908                             while((str=bfr.readLine())!=null)  
 909                             {   editArea.append(str);  
 910                             }  
 911                             this.setTitle(fileName.getName()+" - 记事本");  
 912                             statusLabel.setText(" 当前打开文件:"+fileName.getAbsoluteFile());  
 913                             fr.close();  
 914                             isNewFile=false;  
 915                             currentFile=fileName;  
 916                             oldValue=editArea.getText();  
 917                         }  
 918                         catch (IOException ioException)  
 919                         {  
 920                         }  
 921                     }  
 922                 }  
 923                 else  
 924                 {   return;  
 925                 }  
 926             }  
 927             else  
 928             {   String str=null;  
 929                 JFileChooser fileChooser=new JFileChooser();  
 930                 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
 931                 fileChooser.setDialogTitle("打开文件");  
 932                 int result=fileChooser.showOpenDialog(this);  
 933                 if(result==JFileChooser.CANCEL_OPTION)  
 934                 {   statusLabel.setText(" 您没有选择任何文件 ");  
 935                     return;  
 936                 }  
 937                 File fileName=fileChooser.getSelectedFile();  
 938                 if(fileName==null || fileName.getName().equals(""))  
 939                 {   JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);  
 940                 }  
 941                 else  
 942                 {   try  
 943                     {   FileReader fr=new FileReader(fileName);  
 944                         BufferedReader bfr=new BufferedReader(fr);  
 945                         editArea.setText("");  
 946                         while((str=bfr.readLine())!=null)  
 947                         {   editArea.append(str);  
 948                         }  
 949                         this.setTitle(fileName.getName()+" - 记事本");  
 950                         statusLabel.setText(" 当前打开文件:"+fileName.getAbsoluteFile());  
 951                         fr.close();  
 952                         isNewFile=false;  
 953                         currentFile=fileName;  
 954                         oldValue=editArea.getText();  
 955                     }  
 956                     catch (IOException ioException)  
 957                     {  
 958                     }  
 959                 }  
 960             }  
 961         }  
 962         else if(e.getSource()==fileMenu_Save)  
 963         {   editArea.requestFocus();  
 964             if(isNewFile)  
 965             {   String str=null;  
 966                 JFileChooser fileChooser=new JFileChooser();  
 967                 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
 968                 fileChooser.setDialogTitle("保存");  
 969                 int result=fileChooser.showSaveDialog(this);  
 970                 if(result==JFileChooser.CANCEL_OPTION)  
 971                 {   statusLabel.setText("您没有选择任何文件");  
 972                     return;  
 973                 }  
 974                 File saveFileName=fileChooser.getSelectedFile();  
 975                 if(saveFileName==null || saveFileName.getName().equals(""))  
 976                 {   JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);  
 977                 }  
 978                 else   
 979                 {   try  
 980                     {   FileWriter fw=new FileWriter(saveFileName);  
 981                         BufferedWriter bfw=new BufferedWriter(fw);  
 982                         bfw.write(editArea.getText(),0,editArea.getText().length());  
 983                         bfw.flush();  
 984                         bfw.close();  
 985                         isNewFile=false;  
 986                         currentFile=saveFileName;  
 987                         oldValue=editArea.getText();  
 988                         this.setTitle(saveFileName.getName()+" - 记事本");  
 989                         statusLabel.setText("当前打开文件:"+saveFileName.getAbsoluteFile());  
 990                     }  
 991                     catch (IOException ioException)  
 992                     {  
 993                     }  
 994                 }  
 995             }  
 996             else  
 997             {   try  
 998                 {   FileWriter fw=new FileWriter(currentFile);  
 999                     BufferedWriter bfw=new BufferedWriter(fw);  
1000                     bfw.write(editArea.getText(),0,editArea.getText().length());  
1001                     bfw.flush();  
1002                     fw.close();  
1003                 }                             
1004                 catch(IOException ioException)  
1005                 {                     
1006                 }  
1007             }  
1008         }
1009         else if(e.getSource()==fileMenu_SaveAs)  
1010         {   editArea.requestFocus();  
1011             String str=null;  
1012             JFileChooser fileChooser=new JFileChooser();  
1013             fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
1014             fileChooser.setDialogTitle("另存为");  
1015             int result=fileChooser.showSaveDialog(this);  
1016             if(result==JFileChooser.CANCEL_OPTION)  
1017             {   statusLabel.setText(" 您没有选择任何文件");  
1018                 return;  
1019             }                 
1020             File saveFileName=fileChooser.getSelectedFile();  
1021             if(saveFileName==null||saveFileName.getName().equals(""))  
1022             {   JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);  
1023             }     
1024             else   
1025             {   try  
1026                 {   FileWriter fw=new FileWriter(saveFileName);  
1027                     BufferedWriter bfw=new BufferedWriter(fw);  
1028                     bfw.write(editArea.getText(),0,editArea.getText().length());  
1029                     bfw.flush();  
1030                     fw.close();  
1031                     oldValue=editArea.getText();  
1032                     this.setTitle(saveFileName.getName()+"  - 记事本");  
1033                     statusLabel.setText(" 当前打开文件:"+saveFileName.getAbsoluteFile());  
1034                 }                         
1035                 catch(IOException ioException)  
1036                 {                     
1037                 }                 
1038             }  
1039         }
1040         else if(e.getSource()==fileMenu_PageSetUp)  
1041         {   editArea.requestFocus();  
1042             JOptionPane.showMessageDialog(this,"Sorry,正在开发中!","提示",JOptionPane.WARNING_MESSAGE);  
1043         }
1044         else if(e.getSource()==fileMenu_Print)  
1045         {   editArea.requestFocus();  
1046             JOptionPane.showMessageDialog(this,"Sorry,正在开发中!","提示",JOptionPane.WARNING_MESSAGE);  
1047         }
1048         else if(e.getSource()==fileMenu_Exit)  
1049         {   int exitChoose=JOptionPane.showConfirmDialog(this,"确定要退出吗?","退出提示",JOptionPane.OK_CANCEL_OPTION);  
1050             if(exitChoose==JOptionPane.OK_OPTION)  
1051             {   System.exit(0);  
1052             }  
1053             else  
1054             {   return;  
1055             }  
1056         }
1057         else if(e.getSource()==editMenu_Undo || e.getSource()==popupMenu_Undo)  
1058         {   editArea.requestFocus();  
1059             if(undo.canUndo())  
1060             {   try  
1061                 {   undo.undo();  
1062                 }  
1063                 catch (CannotUndoException ex)  
1064                 {   System.out.println("Unable to undo:" + ex);  
1065                     //ex.printStackTrace();  
1066                 }  
1067             }  
1068             if(!undo.canUndo())  
1069                 {   editMenu_Undo.setEnabled(false);  
1070                 }  
1071         }
1072         else if(e.getSource()==editMenu_Cut || e.getSource()==popupMenu_Cut)  
1073         {   editArea.requestFocus();  
1074             String text=editArea.getSelectedText();  
1075             StringSelection selection=new StringSelection(text);  
1076             clipBoard.setContents(selection,null);  
1077             editArea.replaceRange("",editArea.getSelectionStart(),editArea.getSelectionEnd());  
1078             checkMenuItemEnabled();  
1079         }
1080         else if(e.getSource()==editMenu_Copy || e.getSource()==popupMenu_Copy)  
1081         {   editArea.requestFocus();  
1082             String text=editArea.getSelectedText();  
1083             StringSelection selection=new StringSelection(text);  
1084             clipBoard.setContents(selection,null);  
1085             checkMenuItemEnabled();
1086         }
1087         else if(e.getSource()==editMenu_Paste || e.getSource()==popupMenu_Paste)  
1088         {   editArea.requestFocus();  
1089             Transferable contents=clipBoard.getContents(this);  
1090             if(contents==null)return;  
1091             String text="";  
1092             try  
1093             {   text=(String)contents.getTransferData(DataFlavor.stringFlavor);  
1094             }  
1095             catch (Exception exception)  
1096             {  
1097             }  
1098             editArea.replaceRange(text,editArea.getSelectionStart(),editArea.getSelectionEnd());  
1099             checkMenuItemEnabled();  
1100         }  
1101         else if(e.getSource()==editMenu_Delete || e.getSource()==popupMenu_Delete)  
1102         {   editArea.requestFocus();  
1103             editArea.replaceRange("",editArea.getSelectionStart(),editArea.getSelectionEnd());  
1104             checkMenuItemEnabled(); 
1105         }  
1106         else if(e.getSource()==editMenu_Find)  
1107         {   editArea.requestFocus();  
1108             find();  
1109         } 
1110         else if(e.getSource()==editMenu_FindNext)  
1111         {   editArea.requestFocus();  
1112             find();  
1113         }
1114         else if(e.getSource()==editMenu_Replace)  
1115         {   editArea.requestFocus();  
1116             replace();  
1117         }  
1118         else if(e.getSource()==editMenu_GoTo)  
1119         {   editArea.requestFocus();  
1120             JOptionPane.showMessageDialog(this,"Sorry,正在开发中!","提示",JOptionPane.WARNING_MESSAGE);  
1121         }
1122         else if(e.getSource()==editMenu_TimeDate)  
1123         {   editArea.requestFocus();  
1124             Calendar rightNow=Calendar.getInstance();  
1125             Date date=rightNow.getTime();  
1126             editArea.insert(date.toString(),editArea.getCaretPosition());  
1127         } 
1128         else if(e.getSource()==editMenu_SelectAll || e.getSource()==popupMenu_SelectAll)  
1129         {   editArea.selectAll();  
1130         } 
1131         else if(e.getSource()==formatMenu_LineWrap)  
1132         {   if(formatMenu_LineWrap.getState())  
1133                 editArea.setLineWrap(true);  
1134             else   
1135                 editArea.setLineWrap(false);  
1136         }  
1137         else if(e.getSource()==formatMenu_Font)  
1138         {   editArea.requestFocus();  
1139             font();  
1140         }  
1141         else if(e.getSource()==viewMenu_Status)  
1142         {   if(viewMenu_Status.getState())  
1143                 statusLabel.setVisible(true);  
1144             else   
1145                 statusLabel.setVisible(false);  
1146         }  
1147           
1148         else if(e.getSource()==helpMenu_AboutNotepad)  
1149         {   editArea.requestFocus();  
1150             JOptionPane.showMessageDialog(this,  
1151                      "  注:
                  
" + 
1152                              
1153                              "         这只是个简单的记事本哦!
",
1154                         "关于记事本·",JOptionPane.INFORMATION_MESSAGE);
1155         }
1156     }
1157   
1158     public void removeUpdate(DocumentEvent e)  
1159     {   editMenu_Undo.setEnabled(true);  
1160     }  
1161     public void insertUpdate(DocumentEvent e)  
1162     {   editMenu_Undo.setEnabled(true);  
1163     }  
1164     public void changedUpdate(DocumentEvent e)  
1165     {   editMenu_Undo.setEnabled(true);  
1166     }
1167   
1168     class UndoHandler implements UndoableEditListener  
1169     {   public void undoableEditHappened(UndoableEditEvent uee)  
1170         {   undo.addEdit(uee.getEdit());  
1171         }  
1172     }  
1173     public static void main(String args[])  
1174     {   NoteTest notepad=new NoteTest();  
1175         notepad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1176     }  
1177 } 

以上是关于记事本的主要内容,如果未能解决你的问题,请参考以下文章

求1个VB写记事本的代码

求:用java写的记事本的代码...

如何用浏览器打开用记事本编写的代码

cmd如何编译记事本中的代码

如何用记事本编程

微信小程序代码片段