源码:
//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;
}
}