follow

Friday, 30 August 2013

Event handling in Applet : JAVA Programming by "Gopal Krishna"

Single Button :


import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class mst extends Applet implements ActionListener
{
TextField t1;
Button b1;
public void init()
{
t1=new TextField(10);
b1=new Button("ok");
add (t1);
add (b1);
b1.addActionListener(this);

}
public void actionPerformed(ActionEvent e)  //error 
{
if (e.getSource()==b1)
{
t1.setText("welcome");
}
}
}


/*

<applet code=mst.class height=800 width=1200>
</applet>

*/




Multi Button :

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class mst extends Applet implements ActionListener
{
TextField t1;
Button b1;
Button b2;
public void init()
{
t1=new TextField(15);
b1=new Button("CLICK IFPAGAL HO KYA ");
b2=new Button("CLICK IF NAHI HO");
add (t1);
add (b1);
add (b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)  //error 
{
if (e.getSource()==b1)
{
t1.setText("PTAA THA");
}
if (e.getSource()==b2)
{
t1.setText("Pagal mt bnaao");
}





}
}


/*

<applet code=mst.class height=800 width=1200>
</applet>

*/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

    public class mst1 extends Applet implements ActionListener
    {
        TextField t1;
        TextField t2;
        TextField t3;
        Button b1;
        Button b2;
       
        public void init()
        {
            t1=new TextField(10);
            t2=new TextField(10);
            t3=new TextField(10);
            b1=new Button("sum");
            b2=new Button("sub");
            add(t1);add(t2);add(t3);
            add(b1);
            add(b2);
            t1.setText("0");
            t2.setText("0");
            t3.setText("0");
            b1.addActionListener(this);
            b2.addActionListener(this);
        }
        public void actionPerformed(ActionEvent e)
        {
             try
            {
                String s1,s2,s;
                int a=0;int b=0;int c=0;int d=0;
                s1=t1.getText();
                s2=t2.getText();
                a=Integer.parseInt(s1);
                b=Integer.parseInt(s2);
                c=a+b;
                d=a-b;
           
       
           
                if (e.getSource()==b1)
            {
           
                t3.setText(""+c);
            }
            if (e.getSource()==b2)
            {
                t3.setText(""+d);
            }
           
            }
            catch (Exception ex)
            {
                System.out.println("kuch to gadbad h");
            }
        }
}


/*

<applet code=mst1.class height=800 width=1200>
</applet>

*/

No comments:

Post a Comment