// Source Mastering Java, Bill Buchanan.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String;
public class chap11_06 extends Applet
implements ActionListener
{
TextField tfield1 = new TextField(20);
TextField tfield2 = new TextField(2);
TextField tfield3 = new TextField(2);
TextField tfield4 = new TextField(20);
Button convert = new Button("Convert");
Button quit = new Button("Quit");
public void init()
{
add(new Label("Enter a string"));
add(tfield1);
add(new Label("Character to replace"));
add(tfield2);
add(new Label("with"));
add(tfield3);
add(new Label("Substituted text:"));
add(tfield4);
add(convert); add(quit);
convert.addActionListener(this);
quit.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
int index,curr;
String str1,str2,str3,str4,str;
str=evt.getActionCommand();
if (str.equals("Convert"))
{
str1=tfield1.getText();
str2=tfield2.getText();
str3=tfield3.getText();
str4=str1.replace(str2.charAt(0),
str3.charAt(0));
tfield4.setText(str4);
}
else if (str.equals("Quit"))
System.exit(0);
}
}
|