我学的第一个代码量较大的GUI方面的java源程序

[code]/****************************************
*程序文件名称:TextFindReplace.java
*功能:字符串的查找与替换
*****************************************/
import java.awt.*;
import java.awt.event.*;

public class TextFindReplace implements MouseListener,WindowListener{
private Frame frObj;
private TextArea taObj;
private TextField tfObj1,tfObj2;
private Button buttonObj1;
private Button buttonObj2;
private Button buttonObj3;
private Panel pObj1,pObj2,pObj3,pObj4,pObj5;
private Label labObj1,labObj2,labObj3;
boolean boolObj=false;
Dialog diaObj;
Label textLab;
Button butObj2=new Button("OK");

public static void main(String[] args){
TextFindReplace examObj=new TextFindReplace();
examObj.create();//通过使用成员方法进行实例化各对象等操作.
}
public void create(){
frObj=new Frame("文本的查找与替换");
taObj=new TextArea();
tfObj1=new TextField();
tfObj2=new TextField();
buttonObj1=new Button("Find");
buttonObj2=new Button("Replace");
buttonObj3=new Button("Exit");
labObj1=new Label("文本区域:");
labObj2=new Label("待查找或替换前的字符串:");
labObj3=new Label("替换后的字符串:");
pObj1=new Panel();
pObj2=new Panel();
pObj3=new Panel();
pObj4=new Panel();
pObj5=new Panel();
pObj1.setLayout(new BorderLayout());//先设置布局,再添加和组件
pObj1.add("North",labObj1);
pObj1.add("Center",taObj);//BorderLayout中"CENTER"大小会随窗口的大小改变

pObj2.setLayout(new BorderLayout());
pObj2.add("North",labObj2);
pObj2.add("Center",tfObj1);

pObj3.setLayout(new BorderLayout());
pObj3.add("North",labObj3);
pObj3.add("Center",tfObj2);

pObj4.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));
pObj4.add(buttonObj1);//(20,20)?前者指对象间水平间距,后者指对象距上边界的距离
pObj4.add(buttonObj2);
pObj4.add(buttonObj3);

pObj5.setLayout(new GridLayout(3,1));//(3,1)3行,1列的等大小布局
pObj5.add(pObj2);
pObj5.add(pObj3);
pObj5.add(pObj4);

frObj.setLayout(new GridLayout(1,2));
frObj.add(pObj1);
frObj.add(pObj5);

buttonObj1.addMouseListener(this);//监听者this指当前对象buttonObj1
buttonObj2.addMouseListener(this);//监听者为实现了相应接口的类对象
buttonObj3.addMouseListener(this);

frObj.addWindowListener(this);
frObj.setSize(400,260);
frObj.setVisible(true);

diaObj=new Dialog(frObj);//***参数是Frame对象***实现Frame和Dialog之间的通信
diaObj.setLayout(new FlowLayout(FlowLayout.CENTER,40,20));
textLab=new Label("");
diaObj.add(textLab);
diaObj.add(butObj2);
butObj2.addMouseListener(this);
diaObj.setSize(200,100);
//diaObj2使用默认false,而不用diaObj2.setVisible(false);且下方已有:diaObj.show();
}

public void mouseClicked(MouseEvent e){
Button butObj=(Button)(e.getSource());//***(Button)(e.getSource())***
if(butObj.getLabel()=="Exit") //***通过getLabel()可获得Button上的文本***
{
System.exit(0);//关闭窗口

}
if(butObj.getLabel()=="Find"||butObj.getLabel()=="Replace")
{
String strObj1=taObj.getText();//***通过getText()可获得TextArea或TextField中的文本内容***
String strObj2=tfObj1.getText();
int matchNum=0;
int cursorPos=taObj.getCaretPosition();//***通过getCaretPosition()可获得TextArea等中的光标位置***
matchFun classObj=new matchFun();//***自定义类matchFun***
if(butObj.getLabel()=="Find")
{
matchNum=classObj.strFind(strObj1,strObj2,cursorPos);
textLab.setText("共找到"+matchNum+"处");
diaObj.show();//隐藏,触发,计算,设值,显示
}
if(butObj.getLabel()=="Replace")
{
String strObj3=tfObj2.getText();
matchNum=classObj.strReplace(strObj1,strObj2,strObj3,cursorPos);
textLab.setText("共替换到"+matchNum+"处");
StringBuffer taText=classObj.repStr;//***
taObj.setText(taText.toString());//***
diaObj.show();//***
}

}
if(butObj.getLabel()=="OK")
{diaObj.hide();
frObj.show();
}
}
public void mouseEntered(MouseEvent e){}//***具体的使用方法及其效果?
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void windowClosing(WindowEvent e ){//***用于窗口控制
System.exit(0);
}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}//***具体的使用方法及其效果?
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeActivated(WindowEvent e){}
}

/*类matchFun用于处理有关字符串查找和替换算法*/
class matchFun{
StringBuffer repStr;
public int strFind(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+=1;
i=i-j+1;
j=0;
}
}
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;
repStr=new StringBuffer(s1);//***为了使用replacea()方法,且repStr被申明为实例量。
while(i<repStr.length()&&j<s2.length())
{
if(repStr.charAt(i)==s2.charAt(j))
{ ++i;++j;
if(j==s2.length())
{
k=k+1;
repStr.replace(i-j,i,s3);//***replace(i-j,i,s3),i-j:起始位置,i:结束位置,s3:替换字符串
j=0;
}
}
else{i=i-j+1;j=0;}
}
return k;
}
}[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值