Skip to main content

SWING PROGRAM TO IMPLEMENTS CALCULATOR


/*
 * 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

Popular posts from this blog

SELECT | INSERT | UPDATE | DELETE CODE IN VB.NET WITH AUTO INCREMENT CONCEPT

Database.vb Imports System.Data.SqlClient Public Class Database     Dim con As New SqlConnection( "Data Source=G20;Initial Catalog=Me;Integrated Security=True" )     Public Sub cnopen()         If con.State = ConnectionState.Open Then             con.Close()         End If         con.Open()     End Sub     Public Sub cnclose()         If con.State <> ConnectionState.Closed Then             con.Close()         End If     End Sub     Public Function Displaygrid( ByVal query As String ) As DataTable         Dim dt As N...

CLASSIFICATION OF OPERATING SYSTEMS

Operating systems can be classified into different types based on their interface, user friendliness, and functionality. Different types of operating systems are: Character user interface (CUI)-based operating systems Graphical user interface (GUI)-based operating systems Single-user operating systems Multi-user operating systems Single-tasking operating systems Multitasking operating systems Real-time operating systems 1. Character User Interface (CUI)-Based Operating Systems: MS-DOS and UNIX (not newer Linux) Such an interface is also called a Command-Line Interface (CLI). 2. Graphical User Interface (GUI)-Based Operating Systems GUI-based operating systems are easy to use because the user can click menus and icons rather than type cryptic commands. Users can use a device called a mouse to select an application, after which the operating system will load and run it. In comparison to a CUI-based operating system, a GUI based operating system is slower and needs ...

LECTURE BREAKUP OF PHP MYSQL

PHP+MYSQL Introduction Basic Syntax Variable & it's Scope I/O & Comment   String & it's Function Operator & Expression Decision Control Case Control Loop Control Arrays & Function Form Handling State Management - Cookies State Management - Session Mail Concept Working with File Date/Time Functions Introduction MySQL Environment Database Concept Datatype Query processing Statements  Query processing based on condition Database Connectivity Database Operation With GUI Import/Export Database