Monday, 11 September 2017

Applet program - calculator

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

public class action extends Applet implements ActionListener
{
    Button add1,sub,mul,div,mod;
    Label l1,l2,l3;
    TextField t1,t2,t3;
    Panel p1;

    public void init()
    {
        l1=new Label("Enter 1st Number: ");
        l2=new Label("Enter 2nd Number: ");
        l3=new Label("Result: ");

        t1=new TextField();
        t2=new TextField();
        t3=new TextField();

        add1=new Button("Add");
        sub=new Button("Sub");
        mul=new Button("Mul");
        div=new Button("Div");
        mod=new Button("Mod");
       
        p1=new Panel();
        p1.setLayout(new GridLayout(6,2));
       
        p1.add(l1);p1.add(t1);
        p1.add(l2);p1.add(t2);
        p1.add(l3);p1.add(t3);
        p1.add(add1);p1.add(sub);
        p1.add(mul);p1.add(div);
        p1.add(mod);
        add(p1);

        add1.addActionListener(this);
        sub.addActionListener(this);
        mul.addActionListener(this);
        div.addActionListener(this);
        mod.addActionListener(this);   

    }   
    public void actionPerformed(ActionEvent ae)
    {
        int a,b;
        a=Integer.parseInt(t1.getText());
        b=Integer.parseInt(t2.getText());

        if(ae.getSource()==add1)
            t3.setText(String.ValueOf(a+b));

        if(ae.getSource()==sub)
            t3.setText(String.ValueOf(a-b));

        if(ae.getSource()==mul)
            t3.setText(String.ValueOf(a*b));

        if(ae.getSource()==div)
            t3.setText(String.ValueOf(a/b));

        if(ae.getSource()==mod)
            t3.setText(String.ValueOf(a%b));
       
    }
}

/*<applet code="action" width=500 height=500></applet>*/

No comments:

Post a Comment

Featured post

Development Of JAVA Program