Friday 14 June 2013

string replace in java awt



import java.awt.*;
import java.awt.event.*;
class StringWork extends Frame implements ActionListener
{
    private String str_msg1,str_msg2,str_replace,str_replacewith;
    private Button btn_change;
    private TextArea txta_show1,txta_show2;
    private Label lbl_replace,lbl_replacewith;
    private TextField txt_replace,txt_replacewith;
    public StringWork()
    {
        setLayout(null);
        setTitle("String Work");
        setSize(500,500);
        setLocation(100,100);
       
        txta_show1=new TextArea();
        txta_show1.setBounds(50,75,300,100);
        add(txta_show1);
       
        lbl_replace=new Label("Replace");
        lbl_replace.setBounds(50,225,100,30);
        add(lbl_replace);
       
        txt_replace=new TextField(20);
        txt_replace.setBounds(150,225,150,30);
        add(txt_replace);
       
        lbl_replacewith=new Label("ReplaceWith");
        lbl_replacewith.setBounds(50,260,100,30);
        add(lbl_replacewith);
       
        txt_replacewith=new TextField(20);
        txt_replacewith.setBounds(150,260,150,30);
        add(txt_replacewith);
       
        btn_change=new Button("Change");
        btn_change.setBounds(325,260,50,30);
        btn_change.addActionListener(this);
        add(btn_change);
       
   
       
        txta_show2=new TextArea();
        txta_show2.setBounds(50,350,300,100);
        add(txta_show2);
       
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==btn_change)
        {
            str_replace=txt_replace.getText();
            str_replacewith=txt_replacewith.getText();
            str_msg1=txta_show1.getText();
             str_msg2 = str_msg1.replace(str_replace, str_replacewith);
            txta_show2.setText(""+str_msg2);
           
           
        }
    }
}
class StringWorkDemo
{
    public static void main(String args[])
    {
        new StringWork().setVisible(true);
    }
}

No comments: