| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 951 人关注过本帖
标题:不知道为什么查找和替换不能用 高手帮忙下
只看楼主 加入收藏
yefeng_laixi
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-4-14
收藏
 问题点数:0 回复次数:4 
不知道为什么查找和替换不能用 高手帮忙下
Textfun.rar (1.67 KB)
查找和替换的功能实现不了
2008-05-17 12:15
yefeng_laixi
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-4-14
收藏
得分:0 
源码:
//Textfun.java


import java.awt.*;
import java.awt.event.*;
public class Textfun implements MouseListener
{
   private Frame feobj;
   private TextArea taobj;
   private TextField tdobj1,tdobj2;
   private Button bnobj1,bnobj2,bnobj3;
   private Panel plobj1,plobj2,plobj3,plobj4,plobj5;
   private Label leobj1,leobj2,leobj3;
   Dialog diaobj;
   Label textlab;
   Button buttobj=new Button("ok");
       public static void main(String args[])
       {
           Textfun textfun=new Textfun();
           textfun.Create();
       }
       public void Create()
       {
           feobj=new Frame("文本的查找和替换");
           taobj=new TextArea();
           tdobj1=new TextField();
           tdobj2=new TextField();
           bnobj1=new Button("查找");
           bnobj2=new Button("替换");
           bnobj3=new Button("退出");
           leobj1=new Label("文本区域");
           leobj2=new Label("待查找或替换的字符");
           leobj3=new Label("替换后的字符");
           plobj1=new Panel();
           plobj1.setLayout(new BorderLayout());
           plobj1.add("North",leobj1);
           plobj1.add("Center",taobj);
           plobj2=new Panel();
           plobj2.setLayout(new BorderLayout());
           plobj2.add("North",leobj2);
           plobj2.add("Center",tdobj1);
           plobj3=new Panel();
           plobj3.setLayout(new BorderLayout());
           plobj3.add("North",leobj3);
           plobj3.add("Center",tdobj2);
           plobj4=new Panel();
           plobj4.setLayout(new FlowLayout());
           plobj4.add(bnobj1);
           plobj4.add(bnobj2);
           plobj4.add(bnobj3);
           plobj5=new Panel();
           plobj5.setLayout(new GridLayout(3,1));
           plobj5.add(plobj2);
           plobj5.add(plobj3);
           plobj5.add(plobj4);
           feobj.setLayout(new GridLayout(1,2));
           feobj.add( plobj1);
           feobj.add( plobj5);
           bnobj1.addMouseListener(this);
           bnobj2.addMouseListener(this);
           bnobj3.addMouseListener(this);
           feobj.addWindowListener(new WindowAdapter()
                                {
                                    public void windowClosing(WindowEvent e)
                                    {System.exit(0);
                                    }
                                });
           feobj.setSize(400,250);
           feobj.setVisible(true);
           diaobj=new Dialog(feobj);
           diaobj.setLayout(new FlowLayout(FlowLayout.CENTER,40,20));
           textlab.setText("");
           diaobj.add(textlab);
           diaobj.add(buttobj);
           buttobj.addMouseListener(this);
           diaobj.setSize(200,100);                          
      }

 public void mouseClicked(MouseEvent e)
       {
           Button butobj=(Button)(e.getSource());
           if(butobj.getLabel()=="退出")
           {
               System.exit(0);
           }
          if(butobj.getLabel()=="查找"||butobj.getLabel()=="替换")
          {
               String str1=taobj.getText();
               String str2=tdobj1.getText();
               int cursorpos=taobj.getCaretPosition();
               int matchnum=0;
               matchFun classobj=new matchFun();
               if(butobj.getLabel()=="查找")
               {
                   //matchnum=classobj.strFun(str1,str2,cursorpos);
                 //  textlab.setText("共找到了"+matchnum+"处");
                   diaobj.setVisible(true);
               }
               if(butobj.getLabel()=="替换")
               {
                   /*String str3=tdobj2.getText();
                   matchnum=classobj.strReplace(str1,str2,str3,cursorpos);
                   textlab.setText("共替换了"+matchnum+"处");
                   StringBuffer stb=classobj.sb;
                   taobj.setText(stb.toString());*/
                   diaobj.setVisible(true);
               }
           }
         if(butobj.getLabel()=="ok")
          {
               diaobj.setVisible(false);
               feobj.setVisible(true);
          }
       }
    public void mousePressed(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
}

class matchFun
{
    StringBuffer sb;
   public int strFun(String s1,String s2,int pos)   
   {
       int i,j,k=0;
       i=pos;
       j=0;
       while(i<s1.length()&&j<s2.length())
       {
         if(s1.charAt(i)==s2.charAt(j))
           {
                 ++i;++j;
                 if(j==s2.length())
                 {
                     k=k+1;
                     j=0;
                     i=i-j+1;
                 }
           }
         else
         {
             i=i-j+1;j=0;
         }
       }
       return k;
   }
   public int strReplace(String s1,String s2,String s3,int pos)
   {
      
       int i,j,k=0;
       i=pos;
       j=0;
       sb=new StringBuffer(s1);
       while(i<sb.length()&&j<s2.length())
       {
         if(sb.charAt(i)==s2.charAt(j))
           {
                 ++i;++j;
                 if(j==s2.length())
                 {
                     k=k+1;
                     j=0;
                     sb.replace(i-j,i,s3);
                 }
           }
         else
         {
             i=i-j+1;j=0;
         }
       }
       return k;  
   }
}
2008-05-17 12:42
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
你是不是查找字串的时候错了?好像有函数的..里面应该提供了泛型方法的

学习需要安静。。海盗要重新来过。。
2008-05-17 13:02
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:0 
我这里面实现了查找

NotePad.rar (3.68 KB)
2008-05-17 14:25
yefeng_laixi
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-4-14
收藏
得分:0 
谢谢
你的很不错
2008-05-17 15:19
快速回复:不知道为什么查找和替换不能用 高手帮忙下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027930 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved