/*
* To change this template, choose Tools |
Templates
* and open the template in the editor.
*/
package Swing;
/**
*
* @author Administrator
*/
import java.awt.Color;
import javax.swing.*;
import
java.awt.event.ActionListener;
import
java.awt.event.*;
public class Calculator
implements ActionListener{
JFrame fr;
JPanel p;
JTextField t1,t2,t3;
JLabel l1,l2,l3;
JButton b1,b2,b3,b4,b5;
Calculator()
{
fr=new JFrame("CALCULATE");
p=new JPanel();
p.setLayout(null);
l1=new JLabel("Enter First Number
: ");
l2=new JLabel("Enter Second Number
: ");
l3=new JLabel("Result : ");
t1=new JTextField(20);
t2=new JTextField(20);
t3=new JTextField(20);
b1=new JButton("add");
b2=new JButton("sub");
b3=new JButton("mul");
b4=new JButton("div");
b5=new JButton("clear");
l1.setBounds(20, 30, 100,25);
t1.setBounds(150,30,100,25);
l2.setBounds(20,60,100,25);
t2.setBounds(150,60,100,25);
l3.setBounds(20,90,100,25);
t3.setBounds(150,90,100,25);
b1.setBounds(20,150,70,25);
b2.setBounds(90,150,70,25);
b3.setBounds(160,150,70,25);
b4.setBounds(230,150,70,25);
b5.setBounds(300,150,70,25);
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(l3);
p.add(t3);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
fr.add(p);
fr.addWindowListener(new winclose());
fr.setSize(800,800);
fr.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
float a,b,res=0;
String r;
String s=e.getActionCommand();
System.out.println(s);
a=Float.parseFloat(t1.getText());
b=Float.parseFloat(t2.getText());
if(s.equals("add"))
{
res=a+b;
}
if(s.equals("sub"))
{
res=a-b;
}
if(s.equals("mul"))
{
res=a*b;
}
if(s.equals("div"))
{
res=a/b;
}
t3.setText(""+res);
System.out.println("Result =
"+res);
if(s.equals("clear"))
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}
public static void main(String args[])
{
Calculator obj=new Calculator();
}
}
class winclose extends
WindowAdapter
{
@Override
public void windowClosing(WindowEvent
e)
{
System.exit(0);
}
}
Comments
Post a Comment