Write a program that performs the basic mathematical operations (+, - , *, /) as Calculator using Layout manager. ( File must be save as: AdditionDemo.java)
import java.awt.event.*;//ActionListener;
//import java.awt.event.ActionEvent;
//import javax.swing.JFrame;
//import javax.swing.JTextField;
//import javax.swing.JLabel;
import javax.swing.*;//JButton;
import java.awt.*;//GridBagLayout;
//import java.awt.GridBagConstraints;
//import java.awt.Insets;
class Addition extends JFrame
{
JButton b1=new JButton("+");
JButton b2=new JButton("-");
JButton b3=new JButton("*");
JButton b4=new JButton("/");
//JButton button5=new JButton("=");
JLabel l1=new JLabel("1st Number: ");
JLabel l2=new JLabel("2nd Number: ");
JLabel l3=new JLabel("Result");
JTextField t1=new JTextField("",10);
JTextField t2=new JTextField("",10);
JTextField t3=new JTextField("",10);
GridBagConstraints c=new GridBagConstraints();
GridBagLayout l=new GridBagLayout();
String s;
int a,b;
Addition()
{
super("Addition of 2 number's");
setLayout(l);
c.gridx=0;
c.gridy=0;
c.insets=new Insets(0,0,0,0);
l.setConstraints(l1,c);
add(l1);
c.gridx=1;
c.gridy=0;
c.insets=new Insets(0,0,0,0);
l.setConstraints(t1,c);
add(t1);
c.gridx=0;
c.gridy=1;
c.insets=new Insets(0,0,0,0);
l.setConstraints(l2,c);
add(l2);
c.gridx=1;
c.gridy=1;
c.insets=new Insets(0,0,0,0);
l.setConstraints(t2,c);
add(t2);
c.gridx=0;
c.gridy=2;
c.insets=new Insets(0,0,0,0);
l.setConstraints(l3,c);
add(l3);
c.gridx=1;
c.gridy=2;
c.insets=new Insets(0,0,0,0);
l.setConstraints(t3,c);
add(t3);
c.gridx=1;
c.gridy=3;
c.insets=new Insets(0,0,0,76);
l.setConstraints(b1,c);
add(b1);
c.gridx=1;
c.gridy=3;
c.insets=new Insets(0,0,0,0);
l.setConstraints(b2,c);
add(b2);
c.gridx=1;
c.gridy=3;
c.insets=new Insets(0,76,0,0);
l.setConstraints(b3,c);
add(b3);
c.gridx=2;
c.gridy=3;
c.insets=new Insets(0,0,0,0);
l.setConstraints(b4,c);
add(b4);
/*c.gridx=1;
c.gridy=3;
c.insets=new Insets(0,76,0,0);
layout.setConstraints(button5,c);
add(button5);*/
Cal p=new Cal();
b1.addActionListener(p);
b2.addActionListener(p);
b3.addActionListener(p);
b4.addActionListener(p);
//button5.addActionListener(p);
}
class Cal implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//if(event.getActionCommand()=="+"||event.getActionCommand()=="-"||event.getActionCommand()=="*"||event.getActionCommand()=="/")
//{
a=Integer.valueOf(t1.getText());
s=event.getActionCommand();
//}
//if(event.getActionCommand()=="=")
//{
b=Integer.valueOf(t2.getText());
if(s=="+")
t3.setText(String.valueOf(a)+"+"+String.valueOf(b)+"="+String.valueOf(a+b));
if(s=="-")
t3.setText(String.valueOf(a)+"-"+String.valueOf(b)+"="+String.valueOf(a-b));
if(s=="*")
t3.setText(String.valueOf(a)+"*"+String.valueOf(b)+"="+String.valueOf(a*b));
if(s=="/")
t3.setText(String.valueOf(a)+"/"+String.valueOf(b)+"="+String.valueOf(a/b));
//}
}
}
}
class AdditionDemo
{
public static void main(String args[])
{
Addition addition=new Addition();
addition.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addition.setSize(400,300);
addition.setVisible(true);
}
}
////////////////////////////////////////////
Copy & paste this code in your Textpad & run, then you will get output.......
If you have any problem please comment below.........
Copy & paste this code in your Textpad & run, then you will get output.......
If you have any problem please comment below.........