Recently in the study of Java Swing programming, with the Java reference book to write a string replacement program, put the code to share with you, if there is something wrong, but also look.
Text.java file
Import javax.swing.*;import java.awt.*;import java.awt.event.*;p ublic 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");p ublic text () {settitle ("Replace and find text"); 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 (" text area (looking at the cursor): "); Labobj2=new JLabel ("string before find or replace:"), Labobj3=new JLabel ("replaced string");p obj1=new JPanel ();p obj2=new JPanel ();p obj3=new JPanel ();p obj4=new JPanel ();p obj5=new JPanel ();p obj1.setlayout (New BorderLayout ());p Obj1.add ("North", Labobj1);p Obj1.add ("Center", Scroller);p obj2.setlayout (New BorderLayout ());p Obj2.add ("North", LabObj2);p Obj2.add (" Center ", tfObj1);p obj3.setlayout (New BorderLayout ());p Obj3.add (" North ", LabObj3);p obj3.add (" center ", tfObj2); Pobj4.setlayout (New GridLayout (1,3));p Obj4.add (btnfind);p obj4.add (btnreplace);//panel PObj4 for controlling find, The relative position of the Replace and Exit buttons Pobj4.add (btnexit);p obj5.setlayout (New GridLayout (3,1));p Obj5.add (pObj2);p obj5.add (POBJ3); Pobj5.add (POBJ4); setlayout (new GridLayout); add (pObj1); add (pObj5); validate ();// Ensure that the component has a valid layout btnfind.addactionlistener (this), Btnreplace.addactionlistener (This), Btnreplace.addactionlistener (this );d iaobj=new JDialog (this);d iaobj.setlayout (New FlowLayout (flowlayout.center,40,40)), Textlab=new JLabel (""); Diaobj.add (Textlab);d iaobj.add (BUTOBJ2); Butobj2.addactionlistener (this);//Create a dialog box that, when the user clicks the Find or Replace button, Diaobj.setsize (200, 200); Displays a dialog box that renders the result information, with a label component//And an OK button on the dialog box, which is used to display the number of times to find or replace a string}public void actionperformed (ActionEvent e) { JButton butobj= (JButton) (E.getsource());//Gets the event source button 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;//the number of string matching words, the initial value is 0//int cursorpos=tarea.getcaretposition (); statistic int Cursorpos=0;matchfun classobj=new matchfun () from where the cursor starts, if (butobj.getlabel () = = "Find") {//system.out.println (" Find: "+str1"); Matchnum=classobj.strfind (Str1,str2,cursorpos); Textlab.settext ("Find" +matchnum+ "at all");d Iaobj.show () ;//Displays the Find Results dialog box}if (butobj.getlabel () = = "Replace") {String str3=tfobj2.gettext (); matchnum = Classobj.strreplace (str1, Str2,str3,cursorpos), Textlab.settext ("Total replacement to" +matchnum+ ");d iaobj.show (); StringBuffer Tatext=classobj.repstr;tarea.settext (tatext.tostring ());}}}
Matchfun.java file
/* Class Matchfun is used to process algorithms for string lookups and substitutions */public class Matchfun {stringbuffer repstr;public int strfind (string s1,string s2,int pos {//used to implement string lookups, returns the number of occurrences of the int i,j,k=0;i=pos;j=0;//variable i and J represent the position of the current string in the main string and pattern string,//k represents the number of matches, and POS represents the position in the main string where the comparison was started 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;//will re-assign J to start comparing}}else{i=i-j+1;j=0;}} SYSTEM.OUT.PRINTLN ("returned k=" +k); return k;}}
Textfindreplace.java Main Program
public class Textfindreplace {public static void main (string[] args) {new text ();}}
After running for
Click Find
Click Replace and the program becomes (where "I" Changes to "you")
Text Replacement Program