java实现记事本之Java记事本源码及实现

  • Post author:
  • Post category:java

今天给大家介绍一下关于如何用Java实现记事本功能,是学习Java swing的一个非常好的案例,下面先看一下运行结果(下载地址在最下面):


下面我们来看源码:

[java] 
view plain
 copy

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