java记事本

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.Date;
import java.util.Locale;
import java.text.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.undo.*;
import javax.swing.event.*;
import javax.swing.KeyStroke;
import java.awt.event.InputEvent;

public class MyWord extends JFrame{
 final UndoManager undo = new UndoManager();  
 public final static int CORRECT = 0;
 public final static int MISSED_ONE_LETTER = 1;
 public final static int ADDED_ONE_LETTER = 2;
 public final static int EXCHANGED_ADJACEMENT_LETTERS = 3;
 public final static int UNDEFINED_ERROR = 4;
 private int width,height,status,save_status;
 private Container container;
 private JTextArea textarea;
 private JMenuBar menubar;
 private JFrame frame;
 private int fangxiangStatus=0;
 private String fileDictionary,myfileName;
 private JScrollPane scroll;
 private JPopupMenu popup;
 private JMenu wenjian,bianji,geshi,chakan,bangzhu;
 private String copy,searchWord;
 private static int start=0,checkStart=0;
 private static int huanhang_count=0;
 private JMenuItem wenjian_item[],bianji_item[],geshi_item,check_item,chakan_item[],bangzhu_item[],popup_item[],huanhang_item;
 public MyWord()
 {
  super("记事本");
  
  Toolkit   tk=Toolkit.getDefaultToolkit();    
  Image   image=tk.getImage("jishiben.jpg");   //设置图表 
  this.setIconImage(image);
  
  container=getContentPane();  
  container.setLayout(new BorderLayout());
  actionEventHandler actionHandler=new actionEventHandler();
  frame=new JFrame();
  width=750;
  height=500;
  status=0;
  save_status=0;
  
  menubar=new JMenuBar();
  popup=new JPopupMenu();
  container.add(menubar,BorderLayout.NORTH);   
  
  addWindowListener(
    new WindowAdapter(){
     public void windowClosing(WindowEvent event)
     {
      if(save_status==0)
      {
       int check=JOptionPane.showConfirmDialog(null,"文件的文字已经改变。想保存文件吗?","警告",JOptionPane.YES_NO_CANCEL_OPTION);
       if(check==0)
       {
        saveFile(status);
       }
       if(check==1)
       {
        System.exit(0);
       }
       if(check==2)
       {
        dispose();
       }
      }
      
     }
    
    });
  
  wenjian=new JMenu("文件(F)");
  wenjian.setMnemonic('F');
  wenjian.setFont(new Font("Monospaced",Font.PLAIN,12));
  char wenjians[]={'N','O','S','A','U','P','X'};
  String wenjianOperation[]={"新建(N)","打开(O)...","保存(S)","另存为(A)...","页面设置(U)...","打印(P)","退出(X)"};
  wenjian_item=new JMenuItem[wenjianOperation.length];
  for(int count=0;count<wenjianOperation.length;count++)
  {
   wenjian_item[count]=new JMenuItem(wenjianOperation[count]);
   wenjian_item[count].setBackground(Color.white);
   wenjian_item[count].setFont(new Font("Monospaced",Font.PLAIN,12));
   wenjian_item[count].setMnemonic(wenjians[count]);
   wenjian.add(wenjian_item[count]);
   wenjian_item[count].addActionListener(actionHandler);
   if(count==3||count==5)
   wenjian.addSeparator();
  }
  menubar.add(wenjian);
  
  wenjian_item[0].setAccelerator(KeyStroke.getKeyStroke('N',InputEvent.CTRL_DOWN_MASK));
  
  wenjian_item[1].setAccelerator(KeyStroke.getKeyStroke('O',InputEvent.CTRL_DOWN_MASK));
    
  wenjian_item[2].setAccelerator(KeyStroke.getKeyStroke('S',InputEvent.CTRL_DOWN_MASK));
  wenjian_item[5].setAccelerator(KeyStroke.getKeyStroke('P',InputEvent.CTRL_DOWN_MASK));
  
  
  
  
  
  bianji=new JMenu("编辑(E)");
  bianji.setMnemonic('E');
  bianji.setFont(new Font("Monospaced",Font.PLAIN,12));
  char bianjis[]={'U','T','C','P','L','F','N','R','G','A','D'};
  char accelerator[]={'Z','X','C','V'};
  String bianjiOperation[]={"撤销(U)","剪切(T)","复制(C)","粘贴(P)","删除(L)","查找(F)","查找下一个(N)","替换(R)...","转到(G)...","全选(A)","时间/日期(D)"};
  bianji_item=new JMenuItem[bianjiOperation.length];
  for(int count=0;count<bianjiOperation.length;count++)
  {
   bianji_item[count]=new JMenuItem(bianjiOperation[count]);
   bianji_item[count].setBackground(Color.white);
   bianji_item[count].setFont(new Font("Monospaced",Font.PLAIN,12));
   bianji_item[count].setMnemonic(bianjis[count]);
   
    bianji.add(bianji_item[count]);
   if(count<accelerator.length)
    bianji_item[count].setAccelerator(KeyStroke.getKeyStroke(accelerator[count],InputEvent.CTRL_DOWN_MASK));
   bianji_item[count].addActionListener(actionHandler);
   if(count==0||count==4||count==8)
   bianji.addSeparator();
  }
  menubar.add(bianji);
  bianji_item[4].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
  bianji_item[5].setAccelerator(KeyStroke.getKeyStroke('F',InputEvent.CTRL_DOWN_MASK));
  bianji_item[6].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,0));
  bianji_item[7].setAccelerator(KeyStroke.getKeyStroke('H',InputEvent.CTRL_DOWN_MASK));
  bianji_item[8].setAccelerator(KeyStroke.getKeyStroke('G',InputEvent.CTRL_DOWN_MASK));
  bianji_item[9].setAccelerator(KeyStroke.getKeyStroke('A',InputEvent.CTRL_DOWN_MASK));
  bianji_item[10].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0));
  
  geshi=new JMenu("格式(O)");  
  geshi.setMnemonic('O');
  geshi.setFont(new Font("Monospaced",Font.PLAIN,12));
  
  huanhang_item=new JMenuItem("  自动换行(W)");
  huanhang_item.setMnemonic('W');
  huanhang_item.setBackground(Color.white);
  huanhang_item.addActionListener(actionHandler);
  huanhang_item.setFont(new Font("Monospaced",Font.PLAIN,12));
  
  check_item=new JMenuItem("  拼写检查(S)");
  check_item.setMnemonic('S');
  check_item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F7,0));
  check_item.setBackground(Color.white);
  check_item.setFont(new Font("Monospaced",Font.PLAIN,12));
  check_item.addActionListener(actionHandler);
    
  geshi_item=new JMenuItem("  字体(F)...");
  geshi_item.setBackground(Color.white);
  geshi_item.setFont(new Font("Monospaced",Font.PLAIN,12));
  geshi_item.setMnemonic('F');
  geshi_item.addActionListener(actionHandler);
  
  geshi.add(huanhang_item);
  geshi.add(check_item);
  geshi.add(geshi_item);
  
  
  menubar.add(geshi);
  
  
     
          
  
  
  chakan=new JMenu("查看(V)");
  chakan.setMnemonic('V');
  chakan.setFont(new Font("Monospaced",Font.PLAIN,12));
  String chakanOperation[]={"状态栏(S)"};
  char chakans[]={'S'};
  chakan_item=new JMenuItem[chakanOperation.length];
  for(int count=0;count<chakanOperation.length;count++)
  {
   chakan_item[count]=new JMenuItem(chakanOperation[count]);
   chakan_item[count].setBackground(Color.white);
   chakan_item[count].setFont(new Font("Monospaced",Font.PLAIN,12));
   chakan_item[count].setMnemonic(chakans[count]);
   chakan_item[count].addActionListener(actionHandler);
   chakan.add(chakan_item[count]);
   
  }
  menubar.add(chakan);
  
  
  bangzhu=new JMenu("帮助(H)");
  bangzhu.setMnemonic('H');
  bangzhu.setFont(new Font("Monospaced",Font.PLAIN,12));
  String bangzhuOperation[]={"帮助主题(H)","关于记事本(A)"};
  char bangzhus[]={'H','A'};
  bangzhu_item=new JMenuItem[bangzhuOperation.length];
  
  for(int count=0;count<bangzhuOperation.length;count++)
  {
   bangzhu_item[count]=new JMenuItem(bangzhuOperation[count]);
   bangzhu_item[count].setBackground(Color.white);
   bangzhu_item[count].setFont(new Font("Monospaced",Font.PLAIN,12));
   bangzhu_item[count].setMnemonic(bangzhus[count]);
   bangzhu.add(bangzhu_item[count]);
   bangzhu_item[count].addActionListener(actionHandler);
   if(count==0)
   bangzhu.addSeparator();
  }
    
  menubar.add(bangzhu);
  
  String popupOperation[]={"撤销(U)","剪贴(T)","复制(C)","粘贴(P)","删除(D)","全选(A)"};
  char popupMnemonic[]={'U','T','C','P','D','A'};
  popup_item=new JMenuItem[popupOperation.length];
  for(int count=0;count<popupOperation.length;count++)
  {
   popup_item[count]=new JMenuItem(popupOperation[count]);
   popup_item[count].setBackground(Color.white);
   popup_item[count].setFont(new Font("Monospaced",Font.PLAIN,12));
   popup_item[count].setMnemonic(popupMnemonic[count]);
   popup_item[count].addActionListener(actionHandler);
   if(count==1||count==5)
    popup.addSeparator();
   popup.add(popup_item[count]);
  }

  
  textarea=new JTextArea();
  textarea.setWrapStyleWord(true);
  textarea.setFont(new Font("Monospaced",Font.PLAIN,14));
  textarea.addMouseListener(
    new MouseAdapter(){
     public   void   mousePressed(MouseEvent   event)
     {
      if(event.getButton()==event.BUTTON3)
       checkForTriggerEvent(event);
               
     }          
      
     public   void   mouseReleased(MouseEvent   event)
     {       
      if(event.getButton()==event.BUTTON3)
       checkForTriggerEvent(event);
     }
     
     private void checkForTriggerEvent(MouseEvent event)
     {
      if(event.isPopupTrigger())
       popup.show(textarea, event.getX(), event.getY());
     }
    }
    );
  textarea.addKeyListener(new KeyListener(){
      public void keyTyped(KeyEvent e){
       if(status==1)
       status=0;
     
      }
      public void keyPressed(KeyEvent e){
       
      }
      public void keyReleased(KeyEvent e){
       
      }
     });
  textarea.addMouseListener(new MouseAdapter()
  {
   public void MousePressed(MouseEvent event)
   {
    
    
   }
   public void MouseClicked(MouseEvent event)
   {
    
   }
   public void MouseReleased(MouseEvent event)
   {
    
   }
   public void MouseEntered(MouseEvent event)
   {}
   public void MouseExited(MouseEvent event)
   {}
   
   
  });
  
  Document doc = textarea.getDocument(); 
  doc.addUndoableEditListener(new UndoableEditListener() {
         public void undoableEditHappened(UndoableEditEvent evt) {
             undo.addEdit(evt.getEdit());
         }
     });

  
  scroll=new JScrollPane(textarea);
  scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  
  scroll.setBounds(0,20,width, height-53);
  scroll.setVisible(true);
  
  container.add(scroll,BorderLayout.CENTER);
  
  
  
  setSize(width+5,height);
  setVisible(true);  
 }
 private class actionEventHandler implements ActionListener
 {
  public void actionPerformed(ActionEvent event)
  {
   if(event.getSource()==wenjian_item[0])
   {
    
    if(status==0)
     save_as_File(); 
    else
     saveFile(status);
     
    textarea.setText("");
    mysetTitle("记事本");
    start=0;
    status=0;
    
   }
   if(event.getSource()==wenjian_item[1])
   {
    status=openFile(true);
    start=0;
   }
   if(event.getSource()==wenjian_item[2])
    saveFile(status);
   if(event.getSource()==wenjian_item[3])
    save_as_File();
   if(event.getSource()==wenjian_item[6])
    System.exit(0);
   if(event.getSource()==bianji_item[0])
   {
     try {
                     if (undo.canUndo()) {
                         undo.undo();
                     }
                 } catch (CannotUndoException e) {
                 }
 
   }
   if(event.getSource()==bianji_item[1])
   {
    copy=textarea.getSelectedText();
    textarea.cut();    
   }
   if(event.getSource()==bianji_item[2])
    copy=textarea.getSelectedText();
   if(event.getSource()==bianji_item[3])
    textarea.insert(copy, textarea.getCaretPosition());
   if(event.getSource()==bianji_item[4])
    textarea.cut();
   if(event.getSource()==bianji_item[5])
   {
    SearchDialog search=new SearchDialog(frame);
    frame.setSize(300, 200);
    search.setVisible(true);
   }
   if(event.getSource()==bianji_item[6])
   {
    int index;
    
    int length;
    try{
     length=searchWord.length();
    }catch(Exception e){
     SearchDialog search=new SearchDialog(frame);
     frame.setSize(300, 200);
     search.setVisible(true);
     return;
    }
    
             String tmp=textarea.getText().replaceAll("/r/n","");
             tmp=tmp.replaceAll("/r","");
             tmp=tmp.replaceAll("/t","");
             
             try{
              if(fangxiangStatus==0)      
               index=tmp.indexOf(searchWord,start+length);               
              else if(fangxiangStatus==1)
               index=tmp.lastIndexOf(searchWord,start-length);
              else
               index=-1;
             if(index==-1)
              JOptionPane.showMessageDialog(null, "找不到"+searchWord, "温馨提示",JOptionPane.INFORMATION_MESSAGE);
             else{
             start=index;
             textarea.setSelectedTextColor(Color.blue);
             
             textarea.select(index,index+length);
             }
             }catch(Exception  e){  
                   JOptionPane.showMessageDialog(null, "找不到您想要查找的词!", "温馨提示",JOptionPane.INFORMATION_MESSAGE);
               }  
             
             
   }
   if(event.getSource()==bianji_item[7])
   {
    ReplaceDialog replace=new ReplaceDialog(frame);
    frame.setSize(300, 200);
    replace.setVisible(true);
   }
   if(event.getSource()==bianji_item[9])
    copy=selectAll();
   if(event.getSource()==bianji_item[10])
    getTime();
   if(event.getSource()==popup_item[0])
   {
     try {
                     if (undo.canUndo()) {
                         undo.undo();
                     }
                 } catch (CannotUndoException e) {
                 }
   }
   if(event.getSource()==popup_item[1])
   {
    copy=textarea.getSelectedText();
    textarea.cut(); 
   }
   if(event.getSource()==popup_item[2])
    copy=textarea.getSelectedText();
          if(event.getSource()==popup_item[3])
           textarea.insert(copy, textarea.getCaretPosition());
          if(event.getSource()==popup_item[4])
           textarea.cut();
          if(event.getSource()==popup_item[5])
           copy=selectAll();
          if(event.getSource()==geshi_item)
          {
           StyleDialog style=new StyleDialog(frame);
           frame.setSize(300, 200);          
           style.setVisible(true);           
          }
         if(event.getSource()==check_item)
        {
           CheckDialog check=new CheckDialog(frame);
         frame.setSize(300, 200);          
         check.setVisible(true);           
        }
         if(event.getSource()==bangzhu_item[1])
        {
         AboutDialog about=new AboutDialog(frame);
         frame.setSize(300, 200);          
         about.setVisible(true);           
        }
         if(event.getSource()==bangzhu_item[0])
         {
        try   {  
                java.lang.Runtime.getRuntime().exec("cmd   /c   c://windows//help//notepad.chm");  
                }   catch   (java.io.IOException   e)    
                {  
                                      
                }  
     
         }
         if(event.getSource()==huanhang_item)
         {
        
          if(huanhang_count==0)
          {         
          textarea.setLineWrap(true);
          huanhang_item.setText("√自动换行(W)");
          huanhang_count=1;
          }
          else
          {         
           textarea.setLineWrap(false);
           huanhang_item.setText("  自动换行");
           huanhang_count=0;
          }
         }
           
  }
 }
 private class CheckDialog extends JDialog
 {
  private JLabel label,label2;
  private JTextArea checkArea;
  private DefaultListModel model;
  private JScrollPane check,dictionary;
  private JList dictionaryList;
  private JButton ignore,ignoreAll,addDictionary,change,changeAll,autoChange,cancel;
  private String tmp,tmpString="";
  private String replace="";
  private Vector vector;
  private int flag,length=0,index=0;
  public CheckDialog(JFrame frame)
  {
   super(frame,"拼写检查",false);
   setResizable(false);
   Container container=getContentPane();
   container.setLayout(null);
   
   flag=1;
   start=0;
   vector=new Vector();
   checkActionEventHandler checkHandler=new checkActionEventHandler();
   
   try {     
    
    FileReader fr = new FileReader("Word_storeroom.txt");
        BufferedReader br = new BufferedReader(fr);
        String str =  br.readLine();
      while(str!=null)
      {
       vector.addElement(str);
       str =  br.readLine();
       
      }
  
   }catch(IOException ioexception){
   JOptionPane.showMessageDialog(this, "字库链接失败!", "警告", JOptionPane.ERROR_MESSAGE);
   }
   
   label=new JLabel("不在词典中:");
   label2=new JLabel("建议(N)");
   label.setFont(new Font("宋体",Font.PLAIN,12));
   label2.setFont(new Font("宋体",Font.PLAIN,12));
   label.setBounds(5, 5, 100, 20);
   label2.setBounds(5, 115, 100, 20);
   
   tmp=textarea.getText().replaceAll("/r/n","");
         tmp=tmp.replaceAll("/r","");
         tmp=tmp.replaceAll("/t","");
   
   checkArea=new JTextArea(tmp);
   checkArea.setLineWrap(true);
   checkArea.setSelectedTextColor(Color.red);
   checkArea.setWrapStyleWord(true);
   check=new JScrollPane(checkArea);
   check.setBounds(5, 30, 300, 80);
   check.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
   check.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
   
   model=new DefaultListModel();
   dictionaryList=new JList(model);
   dictionaryList.setVisibleRowCount(5);
   dictionaryList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   
   dictionary=new JScrollPane(dictionaryList);
   dictionary.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
   dictionary.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
   dictionary.setBounds(5, 140, 300, 100);   
   
   ignore=new JButton("继续检查(N)");
   ignore.setMnemonic('N');
   ignore.setFont(new Font("宋体",Font.PLAIN,12));
   ignore.setBounds(310, 30, 120, 20);
   ignore.addActionListener(checkHandler);
   
   ignoreAll=new JButton("忽略(I)");
   ignoreAll.setMnemonic('I');
   ignoreAll.setFont(new Font("宋体",Font.PLAIN,12));
   ignoreAll.setBounds(310, 55, 120, 20);
   ignoreAll.addActionListener(checkHandler);
   
   addDictionary=new JButton("添加到词典(A)");
   addDictionary.setMnemonic('A');
   addDictionary.setFont(new Font("宋体",Font.PLAIN,12));
   addDictionary.setBounds(310, 80, 120, 20);
   addDictionary.addActionListener(checkHandler);
   
   change=new JButton("更改(C)");
   change.setMnemonic('C');
   change.setFont(new Font("宋体",Font.PLAIN,12));
   change.setBounds(310, 140, 120, 20);
   change.addActionListener(checkHandler);
   
   changeAll=new JButton("全部更改(L)");
   changeAll.setMnemonic('L');
   changeAll.setFont(new Font("宋体",Font.PLAIN,12));
   changeAll.setBounds(310, 165, 120, 20);
   changeAll.addActionListener(checkHandler);
   
   autoChange=new JButton("自动更正(R)");
   autoChange.setMnemonic('R');
   autoChange.setFont(new Font("宋体",Font.PLAIN,12));
   autoChange.setBounds(310, 190, 120, 20);
   autoChange.addActionListener(checkHandler);
   
   cancel=new JButton("取消");
   cancel.setFont(new Font("宋体",Font.PLAIN,12));
   cancel.setBounds(310, 215, 120, 20);
   cancel.addActionListener(checkHandler);
   
   
   container.add(label);
   container.add(check);
   container.add(label2);
   container.add(dictionary);
   container.add(ignore);
   container.add(ignoreAll);
   container.add(addDictionary);
   container.add(change);
   container.add(changeAll);
   container.add(autoChange);
   container.add(cancel);
   
   dictionaryList.addListSelectionListener(
     new ListSelectionListener()
     {
      public void valueChanged(ListSelectionEvent event)
      {
       try{
       
       replace=dictionaryList.getSelectedValue().toString();
       }catch(Exception e){}
      }
     }
     );
   
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   this.setLocation(screenSize.width /4 , screenSize.height / 4 );
   
   setSize(440,280);
   setVisible(true);
   
  }
  private class checkActionEventHandler implements ActionListener
  {
   public void actionPerformed(ActionEvent event)
   {
    if(event.getSource()==cancel)
     dispose();
    if(event.getSource()==ignore)
    {
     checkWord();
    }
    if(event.getSource()==change)
     changeWord();
    if(event.getSource()==changeAll)
    {
     String all=textarea.getText();
              try{               
              all=all.replaceAll(replace,textarea.getSelectedText());
              textarea.setText(all);
              checkArea.setText(all);
              }catch(Exception e)
              {}
    }
    if(event.getSource()==addDictionary)
    {
     JOptionPane.showMessageDialog(null, tmpString+"已加入字典", "温馨提示",JOptionPane.INFORMATION_MESSAGE);
    }
    if(event.getSource()==changeAll)
    {
     changeAllWord();
    }
    if(event.getSource()==ignoreAll)
     ignoreWord();
   }
  }
  private void checkWord()
  {
   int count=0,next=0;
   
   StringTokenizer tokens=new StringTokenizer(tmp);
   model.clear();
   while(tokens.hasMoreTokens()&&next<flag)
   {
    tmpString=tokens.nextToken();
    next++;
   }
   try{
   index=tmp.indexOf(tmpString,start+length);
   if(index==-1)
    JOptionPane.showMessageDialog(null, "查询过程出错", "警告",JOptionPane.ERROR);
   else
   {
    textarea.select(index, index+tmpString.length());
    checkArea.select(index, index+tmpString.length());
    JOptionPane.showMessageDialog(this, start+"   "+length, "警告",JOptionPane.ERROR);
   }
   }catch(Exception e){}
   while(count<vector.size())
   {
    int result=compareResult(vector.elementAt(count).toString(),tmpString);
      if(result==1||result==2||result==3)
       model.addElement(vector.elementAt(count).toString());
    count++;
   }
   flag++;
  }
  public void changeWord()
  {
   length=tmpString.length();
         start=index;
   
   textarea.replaceSelection(replace);
   tmp=textarea.getText().replaceAll("/r/n","");
         tmp=tmp.replaceAll("/r","");
         tmp=tmp.replaceAll("/t","");
         checkArea.setText(tmp);
         model.clear();    
         
  }
  public void ignoreWord()
  {
   model.clear();
   start=index;
   length=tmpString.length();
  }
  public void changeAllWord()
  {
   tmp=textarea.getText().replaceAll(tmpString, replace);
   checkArea.setText(tmp);
   textarea.setText(tmp);
  }
  public  int compareResult(String spellWord, String correctWord)
     {
      int i = 0, j = 0;
     
      //找出两字符串第一个不匹配的字符的位置
      while ((i < spellWord.length()) && (j < correctWord.length())) {
       if (spellWord.charAt( i ) != correctWord.charAt( j ))
        break;
       else {
        i++ ; j++ ;
       }
      }
     
      //两字符串完全一样
      if ((i >= spellWord.length()) && (j >= correctWord.length()))
       return CORRECT;
     
      //以下为各种拼写错误的识别过程
      else {
       // 判断是否缺一个字母
       int spellWordPosition = i;
       int correctWordPosition = j + 1;
       while ((spellWordPosition < spellWord.length())
         && (correctWordPosition < correctWord.length())) {
        if (spellWord.charAt( spellWordPosition ) != correctWord.charAt( correctWordPosition ))
         break;
        else {
         spellWordPosition++ ;
         correctWordPosition++ ;
        }
       }
       if ((spellWordPosition >= spellWord.length()) && (correctWordPosition >= correctWord.length()))
        return MISSED_ONE_LETTER;
       //判断是否多一个字母
       spellWordPosition = i + 1;
       correctWordPosition = j ;
       while ((spellWordPosition < spellWord.length())
         && (correctWordPosition < correctWord.length())) {
        if (spellWord.charAt( spellWordPosition ) != correctWord.charAt( correctWordPosition ))
         break;
        else {
         spellWordPosition++ ;
         correctWordPosition++ ;
        }
       }
       if ((spellWordPosition >= spellWord.length()) && (correctWordPosition >= correctWord.length()))
        return ADDED_ONE_LETTER;
       //判断是否相邻字母位置交换了
       if ((i < spellWord.length() - 1) && (j < correctWord.length() - 1)) {
        if ( (spellWord.charAt( i ) == correctWord.charAt( i+1 ))
          && (spellWord.charAt( i+1 ) == correctWord.charAt( i )) ) {
         spellWordPosition = i + 2;
         correctWordPosition = j + 2;
         while ((spellWordPosition < spellWord.length())
           && (correctWordPosition < correctWord.length())) {
          if (spellWord.charAt( spellWordPosition ) != correctWord.charAt( correctWordPosition ))
           break;
          else {
           spellWordPosition++ ;
           correctWordPosition++ ;
          }
         }
         if ((spellWordPosition >= spellWord.length()) && (correctWordPosition >= correctWord.length()))
          return EXCHANGED_ADJACEMENT_LETTERS;
        }
       }
       //如果不是上述情况,则为未定义的拼写错误
       return UNDEFINED_ERROR;
      }
     }
 }
 private void mysetTitle(String tmp)
 {
  this.setTitle(tmp);
 }
 private class ReplaceDialog extends JDialog
 {
  private JLabel neirong,tihuan;
  private JTextField word,mytihuan;
  private JButton search,replace,replaceAll,cancel;
  private JCheckBox daxiao;
  public ReplaceDialog(JFrame frame)
  {
   super(frame,"替换",false);
   Container container=getContentPane();
   container.setLayout(null);
   this.setResizable(false);
   neirong=new JLabel("查找内容(N):");
   tihuan=new JLabel("替换为(P)");
   neirong.setFont(new Font("宋体",Font.PLAIN,12));
   tihuan.setFont(new Font("宋体",Font.PLAIN,12));
   neirong.setBounds(10, 10, 80, 20);
   tihuan.setBounds(10, 40, 80, 20);
   
   word=new JTextField();
   word.setFont(new Font("宋体",Font.PLAIN,12));
   mytihuan=new JTextField();
   mytihuan.setFont(new Font("宋体",Font.PLAIN,12));
   word.setBounds(90, 10, 130, 20);
   mytihuan.setBounds(90, 40, 130, 20);
   
   word.addKeyListener(new KeyListener(){
       public void keyTyped(KeyEvent e){
        
        if(word.getText().length()!=0||!word.getText().equals(""))
        {
        search.setEnabled(true);
        replace.setEnabled(true);
        replaceAll.setEnabled(true);
        }
        else
        {
         search.setEnabled(false);
         replace.setEnabled(false);
         replace.setEnabled(false);
        }
      
       }
       public void keyPressed(KeyEvent e){
        
       }
       public void keyReleased(KeyEvent e){
        
       }
      });
   
   search=new JButton("查找下一个(F)");
   replace=new JButton("替换(R)");
   replaceAll=new JButton("替换全部(A)");
   search.setEnabled(false);
   replace.setEnabled(false);
   replaceAll.setEnabled(false);
   cancel=new JButton("取消");
   search.setMnemonic('F');
   replace.setMnemonic('R');
   replaceAll.setMnemonic('A');
   
   search.setBounds(240, 10, 130, 20);
   replace.setBounds(240, 40, 130, 20);
   replaceAll.setBounds(240, 70, 130, 20);
   cancel.setBounds(240, 100, 130, 20);
   
   daxiao=new JCheckBox("区分大小写(C)");
   daxiao.setMnemonic('C');
   daxiao.setSelected(true);
   daxiao.setBounds(10, 100, 130, 20);
   
   search.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
              int index=0;
              int length=word.getText().length();
              searchWord=word.getText();
              String tmp=textarea.getText().replaceAll("/r/n","");
              tmp=tmp.replaceAll("/r","");
              tmp=tmp.replaceAll("/t","");
              
              try{
              index=tmp.indexOf(searchWord,start+length);
              if(index==-1)
               JOptionPane.showMessageDialog(null, "找不到"+searchWord, "温馨提示",JOptionPane.INFORMATION_MESSAGE);
              else{
              start=index;
              
              textarea.setSelectedTextColor(Color.blue);
              length=searchWord.length();
              textarea.select(index,index+length);
              }
              }catch(Exception  event){  
                    JOptionPane.showMessageDialog(null, "找不到您想要查找的词!", "温馨提示",JOptionPane.INFORMATION_MESSAGE);
                }  
              
              
             }});
   
   cancel.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
              
              dispose();
             }});
   
   replace.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
              int index=0;
              int length=word.getText().length();
              searchWord=word.getText();
              String tmp=textarea.getText().replaceAll("/r/n","");
              tmp=tmp.replaceAll("/r","");
              tmp=tmp.replaceAll("/t","");
              
              try{
              index=tmp.indexOf(searchWord,start+length);
              if(index==-1)
              {
               JOptionPane.showMessageDialog(null, "找不到"+searchWord, "温馨提示",JOptionPane.INFORMATION_MESSAGE);
               return;
              }
              else{
              start=index;
              textarea.setSelectedTextColor(Color.blue);
              length=searchWord.length();
              textarea.select(index,index+length);
              }
              }catch(Exception  event){  
                    JOptionPane.showMessageDialog(null, "找不到您想要查找的词!", "温馨提示",JOptionPane.INFORMATION_MESSAGE);
                }  
              try{
              textarea.replaceSelection(mytihuan.getText());
              }catch(Exception event){}
              
             }});
   replaceAll.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
              String tmp=textarea.getText();
              try{               
              tmp=tmp.replaceAll(word.getText(), mytihuan.getText());
              textarea.setText(tmp);
              }catch(Exception event)
              {}
              
             }});
   container.add(neirong);
   container.add(tihuan);
   container.add(word);
   container.add(mytihuan);
   container.add(search);
   container.add(replace);
   container.add(replaceAll);
   container.add(cancel);
   container.add(daxiao);
   
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
      if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
      }
      if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
      }
      this.setLocation((screenSize.width - frameSize.width) /4, (screenSize.height - frameSize.height) / 4);
   
   setSize(390,170);
   setVisible(true);
  }
 }
 private class SearchDialog extends JDialog
 {
  private JLabel label;
  private JButton search,cancel;
  private JTextField word;
  private JCheckBox daxiaoxie;
  private JPanel fangxiang;
  private JRadioButton up,down;
  private ButtonGroup radioGroup;
  private int daxiaoStatus=0;
  
  private int index=0; 
  private int length=0;
  public SearchDialog(JFrame frame)
  {
   super(frame,"查找",false);
   
   Container container=getContentPane();
   container.setLayout(null);
   this.setResizable(false);
   //start=textarea.getCaretPosition();
   CheckBox check=new CheckBox();
   RadioButtonHandler radio=new RadioButtonHandler();
   
   label=new JLabel("查找内容(N)");
   label.setFont(new Font("宋体",Font.PLAIN,12));
   label.setBounds(10, 10, 80, 20);
   
   word=new JTextField();
   word.setBackground(Color.white);
   word.setFont(new Font("宋体",Font.PLAIN,12));
   word.setBounds(90, 10, 190, 20);
   
   word.addKeyListener(new KeyListener(){
       public void keyTyped(KeyEvent e){
        
        if(word.getText().length()>0||!word.getText().equals(""))
         search.setEnabled(true);
        else
         search.setEnabled(false);
        //JOptionPane.showMessageDialog(null, word.getText().length(), "温馨提示",JOptionPane.INFORMATION_MESSAGE);
       }
       public void keyPressed(KeyEvent e){
        
       }
       public void keyReleased(KeyEvent e){
        if(fangxiangStatus==1)
        {
         String tmp=textarea.getText().replaceAll("/r/n","");
               tmp=tmp.replaceAll("/r","");
               tmp=tmp.replaceAll("/t","");
         start=tmp.length();
        }
        else
         start=0;
       }
      });

   
   search=new JButton("查找下一个(F)");
   search.setEnabled(false);
   search.setFont(new Font("宋体",Font.PLAIN,12));
   search.setBounds(290, 10, 120, 20);
   search.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
              searchWord=word.getText();
              String tmp=textarea.getText().replaceAll("/r/n","");
              tmp=tmp.replaceAll("/r","");
              tmp=tmp.replaceAll("/t","");
              //JOptionPane.showMessageDialog(null, textarea.getCaretPosition(), "温馨提示",JOptionPane.INFORMATION_MESSAGE);
              try{
               if(fangxiangStatus==0)      
                index=tmp.indexOf(searchWord,start+length);               
               if(fangxiangStatus==1)
                index=tmp.lastIndexOf(searchWord,start-length);
               
              if(index==-1)
               JOptionPane.showMessageDialog(null, "找不到"+searchWord, "温馨提示",JOptionPane.INFORMATION_MESSAGE);
              else{
              start=index;
              textarea.setSelectedTextColor(Color.blue);
              length=searchWord.length();
              textarea.select(index,index+length);
              }
              }catch(Exception  event){  
                    JOptionPane.showMessageDialog(null, "找不到您想要查找的词!", "温馨提示",JOptionPane.INFORMATION_MESSAGE);
                }  
              
              
             }});
   
   
   cancel=new JButton("取消");
   cancel.setFont(new Font("宋体",Font.PLAIN,12));
   cancel.setBounds(290, 40, 120, 20);
   cancel.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
              dispose();
             }});
   
   
   daxiaoxie=new JCheckBox("区分大小写(C)");
   daxiaoxie.setSelected(true);
   daxiaoxie.setMnemonic('C');
   daxiaoxie.setFont(new Font("宋体",Font.PLAIN,12));
   daxiaoxie.setBounds(10,70,110,20);
   daxiaoxie.addItemListener(check);
   
   fangxiang=new JPanel();
   fangxiang.setLayout(new BorderLayout());   
   fangxiang.setBorder(BorderFactory.createTitledBorder("方向"));
   fangxiang.setBounds(130,40,150,50);
   
   radioGroup=new ButtonGroup();
      
   up=new JRadioButton("向上(U)");
   up.setMnemonic('U');
   up.addItemListener(radio);
   down=new JRadioButton("向下(D)");
   down.setMnemonic('D');
   down.setSelected(true);
   down.addItemListener(radio);
   up.setFont(new Font("宋体",Font.PLAIN,12));
   down.setFont(new Font("宋体",Font.PLAIN,12));
   fangxiang.add(up,BorderLayout.WEST);
   fangxiang.add(down,BorderLayout.CENTER);
   radioGroup.add(up);
   radioGroup.add(down);
   
   container.add(label);
   container.add(word);
   container.add(search);
   container.add(daxiaoxie);
   container.add(fangxiang);
   container.add(cancel);
   
   

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
      if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
      }
      if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
      }
      this.setLocation((screenSize.width - frameSize.width) /4, (screenSize.height - frameSize.height) / 4);
   
   setSize(430,130);
   setVisible(true);
  }
  private class RadioButtonHandler implements ItemListener
  {
   public void itemStateChanged(ItemEvent event)
   {
    if(event.getSource()==up)
     fangxiangStatus=1;
    if(event.getSource()==down)
     fangxiangStatus=0;
   }
  }
  private class CheckBox implements ItemListener
  {
   public void itemStateChanged(ItemEvent event)
   {
    if(event.getSource()==daxiaoxie)
     if(event.getStateChange()==ItemEvent.SELECTED)
      daxiaoStatus=1;
     else
      daxiaoStatus=0;
   }
  }
 }
 private class AboutDialog extends JDialog
 {
  private JTextArea about;
  private JPanel panel;
  private JButton ok;
  public AboutDialog(JFrame frame)
  {
   super(frame,"关于记事本",false);
   
   Container container=getContentPane();
   container.setLayout(null);
   this.setResizable(false);
   
   String tmp="作者:bluejay/n版本:1.0/n时间:2006年12月9日";

   about=new JTextArea();
   about.setEditable(false);
   about.setLineWrap(true);
   about.setColumns(20);
   about.setFont(new Font("宋体",Font.PLAIN,12));
   about.setBounds(5,5,290,200);
   about.append(tmp+"/n/n/n");
   panel=new JPanel();
   panel.setBackground(Color.white);
   panel.setLayout(null);
   panel.setBounds(0,0,300,300);
   panel.setBorder(BorderFactory.createLoweredBevelBorder());
   panel.add(about);
   
   ok=new JButton("确定(Y)");
   ok.setFont(new Font("Monospaced",Font.PLAIN,12));
   ok.setBounds(110, 230, 80, 20);
   ok.setMnemonic('Y');
   ok.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
              
              dispose();
             }});
   panel.add(ok);
   
   container.add(panel);
   
   
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
      if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
      }
      if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
      }
      this.setLocation((screenSize.width - frameSize.width)/2 , (screenSize.height - frameSize.height)/ 2 );
   setSize(300,300);
   setVisible(true);
  }
 }
 private class StyleDialog extends JDialog
 {
  private JLabel ziti,zixing,daxiao;
  private JPanel style,myshili;
  private JTextArea shili;
  private JButton ok,cancel;
  private JList zitiList,zixingList,daxiaoList;
  private JTextField myziti,myzixing,mydaxiao;
  private String mystyle[]={"Arial","Elephant","Forte","宋体","华文行楷","黑体","华文彩云","华文仿宋","楷书_GB2312","隶书","华文中宋"};
  private String myxingzhuang[]={"常规","斜体","粗体","粗斜体"};
  private String mypounds[]={"9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37"};
  private String daxiao_check,style_check,ziti_check;
  public StyleDialog(JFrame frame)
  {
   super(frame,"字体",false);
   this.setResizable(false);
   this.setBackground(Color.white);
   Container container=getContentPane();
   container.setLayout(null);
   
   ziti=new JLabel("字体");
   ziti.setFont(new Font("Monospaced",Font.PLAIN,12));
   ziti.setBounds(10,10,50,20);
   
   myziti=new JTextField("宋体");
   myziti.setEditable(false);
   myziti.setBackground(Color.white);
   myziti.setBounds(10, 35, 100, 20);
   
   
   zitiList=new JList(mystyle);
   zitiList.setSelectedIndex(3);
   zitiList.setFont(new Font("Monospaced",Font.PLAIN,12));
   zitiList.setVisibleRowCount(5);
   zitiList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   zitiList.addListSelectionListener(new ListSelectionListener()
   {
    public void valueChanged(ListSelectionEvent event)
    {
     myziti.setText(mystyle[zitiList.getSelectedIndex()]);
     ziti_check=myziti.getText();
              style_check=myzixing.getText();
              daxiao_check=mydaxiao.getText();
              int zitidaxiao=Integer.parseInt(daxiao_check);
              int zitistyle=Font.PLAIN;
              if(style_check.equals("常规"))
               zitistyle=Font.PLAIN;
              if(style_check.equals("斜体"))
               zitistyle=Font.ITALIC;
              if(style_check.equals("粗体"))
               zitistyle=Font.BOLD;
              if(style_check.equals("粗斜体"))
               zitistyle=Font.BOLD+Font.ITALIC;
              shili.setFont(new Font(ziti_check,zitistyle,zitidaxiao));
    }
   });
   JScrollPane scroll=new JScrollPane(zitiList);
   scroll.setBounds(10, 60,100 ,100);
   
   zixing=new JLabel("字形");
   zixing.setFont(new Font("Monospaced",Font.PLAIN,12));
   zixing.setBounds(120, 10, 50, 20);
   
   myzixing=new JTextField("常规");
   myzixing.setEditable(false);
   myzixing.setBackground(Color.white);
   myzixing.setBounds(120, 35, 100, 20);
   
   
   zixingList=new JList(myxingzhuang); 
   zixingList.setSelectedIndex(0);
   zixingList.setFont(new Font("Monospaced",Font.PLAIN,12));
   zixingList.setVisibleRowCount(5);
   zixingList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   zixingList.addListSelectionListener(new ListSelectionListener()
   {
    public void valueChanged(ListSelectionEvent event)
    {
    myzixing.setText(myxingzhuang[zixingList.getSelectedIndex()]);
    ziti_check=myziti.getText();
             style_check=myzixing.getText();
             daxiao_check=mydaxiao.getText();
             int zitidaxiao=Integer.parseInt(daxiao_check);
             int zitistyle=Font.PLAIN;
             if(style_check.equals("常规"))
              zitistyle=Font.PLAIN;
             if(style_check.equals("斜体"))
              zitistyle=Font.ITALIC;
             if(style_check.equals("粗体"))
              zitistyle=Font.BOLD;
             if(style_check.equals("粗斜体"))
              zitistyle=Font.BOLD+Font.ITALIC;
             shili.setFont(new Font(ziti_check,zitistyle,zitidaxiao));
     
    }
   });
   JScrollPane scroll2=new JScrollPane(zixingList);
   scroll2.setBounds(120, 60,100 ,80);
   
   daxiao=new JLabel("大小");
   daxiao.setFont(new Font("Monospaced",Font.PLAIN,12));
   
   daxiao.setBounds(230, 10, 50, 20);
   
   mydaxiao=new JTextField("12");
   mydaxiao.setEditable(false);
   mydaxiao.setBackground(Color.white);
   mydaxiao.setBounds(230, 35, 50, 20);
   
   
   daxiaoList=new JList(mypounds); 
   daxiaoList.setSelectedIndex(3);
   daxiaoList.setFont(new Font("Monospaced",Font.PLAIN,12));
   daxiaoList.setVisibleRowCount(5);
   daxiaoList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   daxiaoList.addListSelectionListener(new ListSelectionListener()
   {
    public void valueChanged(ListSelectionEvent event)
    {
     mydaxiao.setText(mypounds[daxiaoList.getSelectedIndex()]);
     ziti_check=myziti.getText();
              style_check=myzixing.getText();
              daxiao_check=mydaxiao.getText();
              int zitidaxiao=Integer.parseInt(daxiao_check);
              int zitistyle=Font.PLAIN;
              if(style_check.equals("常规"))
               zitistyle=Font.PLAIN;
              if(style_check.equals("斜体"))
               zitistyle=Font.ITALIC;
              if(style_check.equals("粗体"))
               zitistyle=Font.BOLD;
              if(style_check.equals("粗斜体"))
               zitistyle=Font.BOLD+Font.ITALIC;
              shili.setFont(new Font(ziti_check,zitistyle,zitidaxiao));
    }
   });
   JScrollPane scroll3=new JScrollPane(daxiaoList);
   scroll3.setBounds(230, 60,50 ,80);
   
   ok=new JButton("确定");
   cancel=new JButton("取消");
   ok.setFont(new Font("Monospaced",Font.PLAIN,12));
   cancel.setFont(new Font("Monospaced",Font.PLAIN,12));
   ok.setBounds(290, 35, 60, 20);
   cancel.setBounds(290,60,60,20);
   cancel.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
              dispose();
             }});
   ok.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
              
              ziti_check=myziti.getText();
              style_check=myzixing.getText();
              daxiao_check=mydaxiao.getText();
              int zitidaxiao=Integer.parseInt(daxiao_check);
              int zitistyle=Font.PLAIN;
              if(style.equals("常规"))
               zitistyle=Font.PLAIN;
              if(style.equals("斜体"))
               zitistyle=Font.ITALIC;
              if(style.equals("粗体"))
               zitistyle=Font.BOLD;
              if(style.equals("粗斜体"))
               zitistyle=Font.BOLD+Font.ITALIC;
              textarea.setFont(new Font(ziti_check,zitistyle,zitidaxiao));
              dispose();
             }});
   
   shili=new JTextArea("2307工作室"); 
   shili.setEditable(false);
   shili.setFont(new Font("Monospaced",Font.PLAIN,12));
   shili.setBorder(BorderFactory.createLoweredBevelBorder());
   shili.setBounds(0, 0, 160, 60);
   style=new JPanel();
   style.setLayout(null);
   style.setBounds(20, 20, 160, 60);
   style.setBorder(BorderFactory.createLoweredBevelBorder());
   style.add(shili);
   
   myshili=new JPanel();
   myshili.setLayout(null);
   myshili.setFont(new Font("Monospaced",Font.PLAIN,12));
   myshili.setBorder(BorderFactory.createTitledBorder("示例"));
   myshili.setBounds(105,165,200,100);
   myshili.add(style);
   
   
   container.add(ziti);
   container.add(myziti);
   container.add(scroll);
   container.add(zixing);
   container.add(myzixing);
   container.add(scroll2);
   container.add(daxiao);
   container.add(mydaxiao);
   container.add(scroll3);
   container.add(ok);
   container.add(cancel);
   container.add(myshili);
   
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
      if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
      }
      if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
      }
      this.setLocation((screenSize.width - frameSize.width) /4, (screenSize.height - frameSize.height) / 4);

   
   setSize(360, 300);   
      setVisible(true);

  }
 }
 private void getTime()
 {
  
  Date currentDate=new Date();
  SimpleDateFormat formatter=new SimpleDateFormat ("yyyy年MM月dd日 HH:mm",Locale.getDefault());
  String time = formatter.format(currentDate);
  textarea.insert(time, textarea.getCaretPosition());
  //JOptionPane.showMessageDialog(this, "position  "+textarea.getCaretPosition(), "出错信息",JOptionPane.ERROR_MESSAGE );
  
 }
 private int openFile(boolean firstTime)
 {
  
  textarea.setText("");
  if(firstTime)
  {
   FileDialog s=new FileDialog(frame);
   s.setMode(FileDialog.LOAD);
   s.setVisible(true);
   fileDictionary=s.getDirectory();
   myfileName=s.getFile();
   if(myfileName!=null)
   this.setTitle(myfileName+"-记事本");
   status=1;
  }
 
  if(myfileName==null||myfileName.equals(""))
  {
   JOptionPane.showMessageDialog(this, "您没有选择任何文件!", "出错信息",JOptionPane.ERROR_MESSAGE );
   return -1;
  }
  else
  {
   try {     
     
     FileReader fr = new FileReader(fileDictionary+myfileName);
         BufferedReader br = new BufferedReader(fr);
         String str =  br.readLine();
       while(str!=null)
       {
        
         textarea.append(str+"/n");
        str =  br.readLine();
        
       }
   
    }catch(IOException ioexception){
    JOptionPane.showMessageDialog(this, "文件打开出错", "警告", JOptionPane.ERROR_MESSAGE);
    }
    return 1;
  }
 }
 public void save_as_File()
 {
  
  FileDialog s=new FileDialog(frame);
  s.setMode(FileDialog.SAVE);
  s.setVisible(true);
  fileDictionary=s.getDirectory();
  myfileName=s.getFile();
  status=1;
  if(myfileName==null||myfileName.equals(""))
   JOptionPane.showMessageDialog(this, "您没有选择任何文件!", "出错信息",JOptionPane.ERROR_MESSAGE );
  else
  {
   try{
    FileOutputStream fos=new FileOutputStream(fileDictionary+myfileName);
    OutputStreamWriter osw=new OutputStreamWriter(fos);
    String saveString=textarea.getText().replaceAll("/n","/r/n");
    osw.write(saveString);
    osw.flush();
    osw.close();

   }catch(IOException ioexception){
    JOptionPane.showMessageDialog(this, "文件打开出错", "警告", JOptionPane.ERROR_MESSAGE);
    }
  }
 }
 public String selectAll()
 {
  
  textarea.selectAll();
  return textarea.getSelectedText();
 }
 public void saveFile(int status)
 {
  if(status==1)
  {
   try{
    FileOutputStream fos=new FileOutputStream(fileDictionary+myfileName);
    OutputStreamWriter osw=new OutputStreamWriter(fos);
    String saveString=textarea.getText().replaceAll("/n","/r/n");
    //textarea.getDocument();
    osw.write(saveString);
    osw.flush();
    osw.close();
    save_status=1;
   }catch(IOException ioexception){
    JOptionPane.showMessageDialog(this, "文件打开出错", "警告", JOptionPane.ERROR_MESSAGE);
    }
  }
  else
  {
   
   save_as_File();
   
  }
  
 }
 public static void main(String args[])
 {
  MyWord application=new MyWord();
  application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值