Search This Blog

Friday, July 08, 2011

Communicate with Two Applet

AppletToApplet.html

<html>
<body>


<applet code="SumClass" width="300" height=300 name="sumapp" >
</applet>


<applet code="AnsClass" width="300" height="300" name="ansapp" >
</applet>

</body>
</html>

SumClass.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="SumClass" width=300 height=300>
</applet>
*/
/*
<applet code="AnsClass" width=300 height=300>
</applet>
*/

public class SumClass extends Applet implements ActionListener
{
     TextField fNo=new TextField();
     TextField sNo=new TextField();
     Label fNoLable=new Label("Enter First No :-",2);
     Label sNoLable=new Label("Enter Second No :-",2);
     Label error=new Label();
     Button sum =new Button("Sum");
   
    int fNumber,sNumber,ans;
   
    public void init()//Override the applet init method which initilized the componet when the applet start at first....
    {
            setLayout(null);
            add(fNo);//add the first text filed in to applet
            add(sNo);//add the second text filed in to applet
            add(fNoLable);//add the first lable in to applet
            add(sNoLable);//add the second lable in to applet
            add(error);
            add(sum);
            fNoLable.setBounds(50,50,100,50);//set Enter First No :- lable at x,y,width,hight
            sNoLable.setBounds(50,90,120,50);//set Enter First No :- lable at x,y,width,hight
            error.setBounds(190,160,120,70);//set Enter First No :- lable at x,y,width,hight
            fNo.setBounds(190,60,80,25);//set first no textfield :- lable at x,y,width,hight
            sNo.setBounds(190,100,80,25);//set second no textfield :- lable at x,y,width,hight
            sum.setBounds(200,140,50,25);//set Button named Sum x,y,width,hight
            sum.addActionListener(this);//binding the ActionListener with Sum named button
    }
   
    public void actionPerformed(ActionEvent ae)//ovrride the ActionListener's  method which occure when the user click on butten named Sum
    {
        String sum=ae.getActionCommand();
       
       
        String ff,sf;
       

       
        try
        {
                ff=fNo.getText();
                sf=sNo.getText();
                if(ff==null||ff.trim().length()==0)
                {
                    error.setText("Enter First Number");
                }
                else
                {
                    error.setText("");
                    fNumber=Integer.parseInt(fNo.getText());
                   
                    if(sf==null||sf.trim().length()==0)
                    {
                        error.setText("Enter Second Number");
                    }
                    else
                    {
                        error.setText("");
                        sNumber=Integer.parseInt(sNo.getText());
                        ans=fNumber + sNumber;
                        //error.setText(Integer.toString(ans));
                       
                        //AnsClass ac=new AnsClass();
                        AnsClass.ans.setText("");
                        AnsClass.ans.setText(Integer.toString(ans));
                       
                    }
                }
               
        }
        catch(NumberFormatException e)
        {
            error.setText("Enter Proper Number");
        }
        catch(ArithmeticException e)
        {
            System.out.println("Problem During The Arithmetic Operation");
        }
        catch(Exception e)
        {
            System.out.println("Error In Your Input Type");
        }
    }
   
   
}

AnsClas.java

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

/*
<applet code="AnsClass" width=200 height=200>
</applet>
*/

public class AnsClass extends Applet
{
    static TextField ans=new TextField();
    public void init()
    {
        add(ans);
    }
}

No comments:

Post a Comment