Java 记事本代码


源文件、文档说明、可运行的jar文件下载地址

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /pub/2011/11/05/Java 记事本代码/

  1. /** 
  2.  * NoteBook 
  3.  * 
  4.  * @versionliujunguang 
  5.  * @version 1.00 09/10/25 
  6.  */  
  7. public class NoteBook {  
  8.     public static void main(String[] args) {  
  9.         // Create application frame.   
  10.         NoteBookFrame frame = new NoteBookFrame();  
  11.         // Show frame   
  12.         frame.setVisible(true);  
  13.     }  
  14. }  

 

 

  1. import java.awt.*;  
  2. /** 
  3.  * NoteBook-OpenClass. 
  4.  * 
  5.  * @liujunguang 
  6.  * @version 1.00 09/10/28 
  7.  */  
  8.   
  9. class  SeeClass extends Frame  
  10.  {  
  11.     NoteBookFrame notebookframe = null;  
  12.     SeeClass(NoteBookFrame  p)  
  13.      {  
  14.         notebookframe = p;  
  15.      }  
  16.     public void Modo()  
  17.      {  
  18.         System.out.println("状态栏");  
  19.     }   
  20. }      
  21. //编辑类中的查找选项的实现   
  22. import javax.swing.*;  
  23. import java.awt.*;  
  24. import java.awt.event.*;  
  25. import java.lang.*;//String 类   
  26. import javax.swing.JOptionPane;  
  27. class SearchFrame extends JFrame implements ActionListener,ItemListener//单选按钮事件接口   
  28.  {  
  29.    JLabel label1,label2,label3;  
  30.    JButton but1,but2,but3,but4;  
  31.    JTextField textfield,text;  
  32.    JRadioButton radiobutton1,radiobutton2;  
  33.    ButtonGroup  group1;  
  34.    JCheckBox box1;  
  35.    NoteBookFrame notebookframe = null;  
  36.    String sub1="",sub2="";  
  37.    int n=-1;  
  38.    boolean isup = false;//判断是否是最后一个找到的内容时用到   
  39.    boolean direction = true ;//判断查找方向  向下为 true   
  40.    SearchFrame(NoteBookFrame  p)  
  41.     {  
  42.      notebookframe = p;  
  43.      label1 = new JLabel("查找内容(N):");  
  44.      label2 = new JLabel("方向");  
  45.      label3 = new JLabel("替换为(P)");  
  46.        
  47.      but1 = new JButton("查找下一个(F)");  
  48.      but2 = new JButton("       取       消      ");  
  49.      but3 = new JButton("       替       换      ");  
  50.      but4 = new JButton("       转       到      ");  
  51.      textfield  = new JTextField(15);  
  52.      text = new JTextField(15);  
  53.        
  54.      radiobutton1 = new JRadioButton("向下(U)",true);  
  55.      radiobutton2 = new JRadioButton("向上(D)");  
  56.        
  57.      group1 = new ButtonGroup();  
  58.      //创建组件   
  59.      box1 = new JCheckBox("区分大小写(C)");  
  60.      Container con = getContentPane();  
  61.       con.setLayout(null);//设置布局为空   
  62.      group1.add(radiobutton1);  
  63.      group1.add(radiobutton2);//将单选按钮加到按钮组中   
  64.        
  65.      con.add(label1);//查找   
  66.      label1.setBounds(10,10,85,25);  
  67.        
  68.      con.add(label3);//替换为   
  69.      label3.setBounds(10,45,85,25);  
  70.       
  71.      con.add(textfield);//文本   
  72.      textfield.setBounds(90,10,180,25);  
  73.        
  74.       con.add(text);//文本   
  75.      text.setBounds(90,45,180,25);  
  76.       
  77.      con.add(but1);//查找下一个   
  78.      but1.setBounds(280,10,120,25);  
  79.       
  80.      con.add(label2);//方向   
  81.      label2.setBounds(135,80,50,25);  
  82.        
  83.      con.add(box1);  
  84.      box1.setBounds(10,115,120,25);  
  85.       
  86.      con.add(radiobutton1);//向下   
  87.      radiobutton1.setBounds(130,115,70,25);  
  88.        
  89.      con.add(radiobutton2);//向上   
  90.      radiobutton2.setBounds(200,115,70,25);  
  91.        
  92.      con.add(but2);//取消   
  93.      but2.setBounds(280,115,120,25);  
  94.        
  95.      con.add(but3);//替换   
  96.      but3.setBounds(280,45,120,25);  
  97.       
  98.      con.add(but4);//转到   
  99.      but4.setBounds(280,80,120,25);  
  100.        
  101.      but1.addActionListener(this);  
  102.      but2.addActionListener(this);  
  103.      but3.addActionListener(this);  
  104.      but4.addActionListener(this);  
  105.      box1.addItemListener(this);  
  106.      textfield.addActionListener(this);  
  107.      text.addActionListener(this);  
  108.      radiobutton1.addItemListener(this);  
  109.      radiobutton2.addItemListener(this);  
  110.      //注册监听   
  111.        
  112.      setBounds(500,300,420,180);  
  113.      setVisible(false);  
  114.      setTitle("查找");  
  115.        
  116.      validate();  
  117.        
  118.     }  
  119.     public void actionPerformed(ActionEvent e)  
  120.       {  
  121.          sub1 = notebookframe.textarea.getText();//得到文本区中的文本   
  122.          sub2 = textfield.getText();//得到文本框中的文本   
  123.          if(!isup) //如果不区分大小写(默认不区分大小写)    
  124.             {  
  125.              sub1 = sub1.toLowerCase();//将sub1转换成小写   
  126.              sub2 = sub2.toLowerCase();//将sub2转换成小写   
  127.             }  
  128.          if(!direction)   n =  sub1.lastIndexOf(sub2);//在文本区中查找文本框中的内容    
  129.          else n =  sub1.indexOf(sub2);//在文本区中查找文本框中的内容   
  130.         if(e.getSource() == but1||e.getSource() == textfield)//查找下一个   
  131.          {  
  132.               
  133.              if(n!=-1)  
  134.               {  
  135.                 notebookframe.toFront();//如果此窗口是可见的,则将此窗口置于前端,并可以将其设为焦点 Window   
  136.                 notebookframe.textarea.select(n,n+sub2.length());//选中查找的内容   
  137.                 this.setVisible(false);  
  138.               }  
  139.              else   
  140.                {  
  141.                 JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE);  
  142.                }     
  143.          }  
  144.         if(e.getSource() == but2)//取消   
  145.          {  
  146.            this.setVisible(false);  
  147.          }   
  148.          if(e.getSource() == but4)//转到   
  149.          {  
  150.           int i=0,j=1;  
  151.           char ch[];  
  152.           try{  
  153.              int raw = Integer.parseInt(textfield.getText());//得到文本框中的文本   
  154.              String s = notebookframe.textarea.getText();  
  155.              ch = new char[s.length()];  
  156.              s.getChars(0,s.length()-1,ch,0);  
  157.               
  158.              while(j<raw)  
  159.                {  
  160.                 i++;  
  161.                 if(ch[i] == '/n')  
  162.                 {j++;}  
  163.                 if(i == s.length())break;  
  164.                }  
  165.             if(raw == 1//转到第一行   
  166.                notebookframe.textarea.setCaretPosition(0);  
  167.                 
  168.             else   
  169.              notebookframe.textarea.setCaretPosition(i+1);//转到指定行   
  170.              this.setVisible(false);  
  171.            }  
  172.            catch(Exception a){  
  173.              JOptionPane.showMessageDialog(this,"你输入的位置不对无法到达!","记事本",JOptionPane.WARNING_MESSAGE);  
  174.            }     
  175.          }    
  176.            
  177.         if(e.getSource() == but3)//替换   
  178.          {   
  179.            if(n != -1)  
  180.               {  
  181.                 String  sub3 = text.getText();  
  182.                 notebookframe.textarea.select(n,n+sub2.length());//选中查找的内容   
  183.                 notebookframe.textarea.replaceRange(sub3,n,n+sub2.length());//替换选中位置的文本   
  184.                   
  185.                 sub1 = notebookframe.textarea.getText();//得到文本区中的文本   
  186.                 sub2 = textfield.getText();//得到文本框中的文本   
  187.                 if(!isup) //如果不区分大小写(默认不区分大小写)    
  188.                   {  
  189.                      sub1 = sub1.toLowerCase();//将sub1转换成小写   
  190.                      sub2 = sub2.toLowerCase();//将sub2转换成小写   
  191.                   }  
  192.                 if(!direction)   n =  sub1.lastIndexOf(sub2);//在文本区中查找最后出现的文本框中的内容      
  193.                 else n =  sub1.indexOf(sub2);//在文本区中查找最先出现的文本框中的内容   
  194.                 notebookframe.toFront();//如果此窗口是可见的,则将此窗口置于前端,并可以将其设为焦点 Window   
  195.                 if(n!=-1 )  
  196.                   {  
  197.                    notebookframe.textarea.select(n,n+sub2.length());//选中查找内容   
  198.                   }  
  199.                 else  
  200.                    JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE);  
  201.                   
  202.               }  
  203.            else   
  204.               {  
  205.                 JOptionPane.showMessageDialog(this,"所指定的文本没有找到无法替换!","记事本",JOptionPane.WARNING_MESSAGE);  
  206.               }     
  207.          }   
  208.          
  209.       }   
  210.     public void nextShear()//查找下一个 菜单项对应的   
  211.        {   
  212.            
  213.            
  214.          if(n!=-1)  
  215.           {  
  216.             sub1 = notebookframe.textarea.getText().substring(n+sub2.length());//得到文本区的子串   
  217.             if(!isup) //如果不区分大小写(默认不区分大小写)    
  218.              {  
  219.                 
  220.               sub1 = sub1.toLowerCase();//将sub1转换成小写   
  221.               sub2 = sub2.toLowerCase();//将sub2转换成小写   
  222.              }  
  223.               
  224.             if(sub1.indexOf(sub2)!=-1)  
  225.              {  
  226.                  
  227.                n =  n+sub2.length()+sub1.indexOf(sub2);//得到查找内容在文本区中的位置   
  228.                notebookframe.textarea.select(n,n+sub2.length());//选中查找内容   
  229.              }  
  230.               
  231.              
  232.             else   
  233.               JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE);  
  234.           }  
  235.          else   
  236.           JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE);  
  237.            
  238.              
  239.             
  240.             
  241.        }  
  242.     public void itemStateChanged(ItemEvent ee)  
  243.       {  
  244.          if(ee.getSource() == box1)  
  245.            {   
  246.              if(box1.isSelected())  
  247.               {  
  248.                 isup = true ;  
  249.               }  
  250.               else   
  251.                 isup = false ;  
  252.            }  
  253.           if(ee.getSource() == radiobutton1)   
  254.             {  
  255.              if(radiobutton1.isSelected())  
  256.               {  
  257.                 direction = true ;  
  258.               }  
  259.             }  
  260.            if(ee.getSource() == radiobutton2)   
  261.             {  
  262.              if(radiobutton2.isSelected())  
  263.               {  
  264.                 direction = false;  
  265.               }  
  266.             }    
  267.       }     
  268.  }   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 下一页

相关内容