最近在学习java的Swing编程,用java参照课本写了个字符串替换的程序,把代码贴出来与大家分享,如有不妥之处,还望指正。
text.java文件
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class text extends JFrame implements ActionListener{
private JTextArea tarea;
private JTextField tfObj1,tfObj2;
private JButton btnFind;
private JButton btnReplace;
private JButton btnExit;
private JPanel pObj1,pObj2,pObj3,pObj4,pObj5;
private JLabel labObj1,labObj2,labObj3;
boolean boolObj=false;
JDialog diaObj;
JLabel textLab;
JButton butObj2=new JButton("OK");
public text(){
setTitle("文本的替换与查找");
setSize(500,200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
tarea = new JTextArea();
JScrollPane scroller = new JScrollPane(tarea);
tfObj1=new JTextField();
tfObj2=new JTextField();
btnFind=new JButton("Find");
btnReplace=new JButton("Replace");
btnExit=new JButton("Exit");
labObj1=new JLabel("文本区域(从光标处开始查找):");
labObj2=new JLabel("待查找或替换前的字符串:");
labObj3=new JLabel("替换后的字符串");
pObj1=new JPanel();
pObj2=new JPanel();
pObj3=new JPanel();
pObj4=new JPanel();
pObj5=new JPanel();
pObj1.setLayout(new BorderLayout());
pObj1.add("North", labObj1);
pObj1.add("Center", scroller);
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 GridLayout(1,3));
pObj4.add(btnFind);
pObj4.add(btnReplace);//面板pObj4用于控制Find、Replace和Exit按钮的相对位置
pObj4.add(btnExit);
pObj5.setLayout(new GridLayout(3,1));
pObj5.add(pObj2);
pObj5.add(pObj3);
pObj5.add(pObj4);
setLayout(new GridLayout(1,2));
add(pObj1);
add(pObj5);
validate();//确保组件有有效的布局
btnFind.addActionListener(this);
btnReplace.addActionListener(this);
btnReplace.addActionListener(this);
diaObj=new JDialog(this);
diaObj.setLayout(new FlowLayout(FlowLayout.CENTER,40,40));
textLab=new JLabel("");
diaObj.add(textLab);
diaObj.add(butObj2);
butObj2.addActionListener(this);//创建一个对话框,当用户单击Find或Replace按钮后,
diaObj.setSize(200, 200); //显示一个呈现结果信息的对话框,对话框上有一个Label组件
//和一个OK按钮,Label组件用于显示查找或替换字符串的次数
}
public void actionPerformed(ActionEvent e){
JButton butObj=(JButton)(e.getSource());//获得事件源按钮
if(butObj.getLabel()=="Exit"){
System.exit(0);
}
if(butObj.getLabel()=="OK"){
diaObj.setVisible(false);
}
if(butObj.getLabel()=="Find"||butObj.getLabel()=="Replace"){
String str1=tarea.getText();
String str2=tfObj1.getText();
int matchNum=0;//存放字符串匹配词数,初值为0
//int cursorPos=tarea.getCaretPosition();从光标开始的地方统计
int cursorPos=0;
matchFun classObj=new matchFun();
if(butObj.getLabel()=="Find"){
//System.out.println("Find:"+str1);
matchNum=classObj.strFind(str1,str2,cursorPos);
textLab.setText("共找到"+matchNum+"处");
diaObj.show();//显示查找结果对话框
}
if(butObj.getLabel()=="Replace"){
String str3=tfObj2.getText();
matchNum = classObj.strReplace(str1,str2,str3,cursorPos);
textLab.setText("共替换到"+matchNum+"处");
diaObj.show();
StringBuffer taText=classObj.repStr;
tarea.setText(taText.toString());
}
}
}
}
matchFun.java文件
/*类matchFun用于处理有关字符串查找和替换的算法*/
public class matchFun {
StringBuffer repStr;
public int strFind(String s1,String s2,int pos){
//用于实现字符串的查找,返回匹配的次数
int i,j,k=0;
i=pos;
j=0;
//变量i和j分别表示主串和模式串中当前字符串的位置,
//k表示匹配次数,pos代表主串中开始比较的位置
while(i<s1.length()&&j<s2.length()){
if(s1.charAt(i)==s2.charAt(j)){
++i;++j;
if(j==s2.length()){
k=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);
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);
j=0;//将j进行重新赋值开始比较
}
}
else{
i=i-j+1;
j=0;
}
}
System.out.println("返回的k="+k);
return k;
}
}
TextFindReplace.java主程序
public class TextFindReplace {
public static void main(String[] args){
new text();
}
}
运行后为
点击Replace,程序变为(其中“我”改变为了“你”)