Manu

Saturday, November 19, 2016

Write a java program to input two values and perform arithmetic operation using button

importjava.applet.Applet;
importjava.awt.Button;
importjava.awt.TextField;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
public class OperatorApplet extends Applet implements ActionListener
{
TextField t1,t2,t3;
    Button b1,b2,b3,b4;
public void init()
    {
setLayout(null);
    t1 = new TextField(15);
t1.setBounds(100,100,50,20);
    t2 = new TextField(15);
t2.setBounds(100,125,50,20);
    t3 = new TextField(15);
t3.setBounds(100,150,50,20);
    b1 = new Button("ADD");
b1.setBounds(50,180,40,20);
    b2 = new Button("DIV");
b2.setBounds(100,180,40,20);

    b3 = new Button("MUL");
b3.setBounds(150,180,40,20);
    b4 = new Button("Sub");
b4.setBounds(200,180,40,20);
add(t1);
add(t2);
add(t3);          
add(b1);
add(b2);
add(b3);
add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
    }
public void actionPerformed(ActionEvent e)
    {
inti,j;
        i=Integer.parseInt(t1.getText());
        j=Integer.parseInt(t2.getText());
if(e.getSource()==b1)
t3.setText(""+(i+j));
else if(e.getSource()==b2)
t3.setText(""+(i/j));
else if(e.getSource()==b3)
t3.setText(""+(i*j));
else
t3.setText(""+(i-j));
    }
}

Output :

No comments:

Post a Comment