Skip to main content
JAVA PROGRAM TO CREATE A FRAME IN WHICH TAKE TWO VALUE FROM USER AND PRINT THEIR RESULT BASED ON ARITHMETIC OPERATION
/*WAP to create a frame in which take two value from user and add button like add , sub , mul .div, and cancel and show their result on the based of user action*/
PROGRAM CODE---
import java.awt.*;
import java.awt.event.*;
class Calculator extends Frame implements ActionListener
{
Frame f;
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1,b2,b3,b4,b5;
public Calculator()
{
f = new Frame("Calculator");
f.setSize(500,500);
f.setVisible(true);
f.setLocation(100,100);
f.setBackground(Color.GRAY);
f.setForeground(Color.BLACK);
f.setLayout(null); //library disable
f.setResizable(false);
l1 = new Label("Enter First Number"); //message label
l2 = new Label("Enter Second Number");
l3 = new Label("your result is :");
t1= new TextField();
t2= new TextField();
t3= new TextField();
b1= new Button("Add");
b2= new Button("Sub");
b3= new Button("Mul");
b4= new Button("Div");
b5= new Button("Cancel");
l1.setBounds(40,50,130,40); //set location
t1.setBounds(300,50,140,30);
l2.setBounds(40,100,130,40);
t2.setBounds(300,100,140,30);
l3.setBounds(40,150,130,40);
t3.setBounds(300,150,140,30);
b1.setBounds(50,250,80,30);
b2.setBounds(140,250,80,30);
b3.setBounds(230,250,80,30);
b4.setBounds(320,250,80,30);
b5.setBounds(410,250,80,30);
f.add(l1);
f.add(l2); //add into frame
f.add(l3);
f.add(t1);
f.add(t2);
f.add(t3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
}
public void actionPerformed(ActionEvent e)
{
int n1=Integer.parseInt(t1.getText());
int n2=Integer.parseInt(t2.getText());
if(e.getSource()==b1)
{
t3.setText(String.valueOf(n1+n2));
}
if(e.getSource()==b2)
{
t3.setText(String.valueOf(n1-n2));
}
if(e.getSource()==b3)
{
t3.setText(String.valueOf(n1*n2));
}
if(e.getSource()==b4)
{
t3.setText(String.valueOf(n1/n2));
}
if(e.getSource()==b5)
{
System.exit(0);
}
}
public static void main(String ar[])
{
new Calculator();
}
}
OUTPUT OF ABOVE PROGRAM---
Comments
Post a Comment