Skip to main content

gui calculator design in java

gui calculator design
/*
// Program for a calculator//

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Calculator extends JFrame implements ActionListener
{
    JTextField txt;
    JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,btn15,btn16,btn17,btn18,btn19;
         String s1="",s2="",s3="",s4="",s5="";
      double n1=0;
      int t=0;
int d=0;
    Calculator()

    {
     

      txt=new JTextField(20);
     
      btn1=new JButton("1");
     
      btn2=new JButton("2");
      btn3=new JButton("3");
      btn4=new JButton("4");
      btn5=new JButton("5");
      btn6=new JButton("6");
      btn7=new JButton("7");
      btn8=new JButton("8");
      btn9=new JButton("9");
      btn10=new JButton("0");
      btn11=new JButton("+");
      btn12=new JButton("-");
      btn13=new JButton("*");
      btn14=new JButton("/");
      btn15=new JButton(".");
      btn16=new JButton("=");
      btn17=new JButton("BACKSPACE");
      btn18=new JButton("CE");
      btn19=new JButton("C");
     
      Container C=getContentPane();
      C.setLayout(new FlowLayout());

      setLayout(null);

      txt.setBounds(100,100,120,20);
      add(txt);
      btn1.setBounds(100,150,50,30);
      add(btn1);
      btn2.setBounds(170,150,50,30);
      add(btn2);
      btn3.setBounds(240,150,50,30);
      add(btn3);
      btn4.setBounds(310,150,50,30);
      add(btn4);
      btn5.setBounds(100,200,50,30);
      add(btn5);
      btn6.setBounds(170,200,50,30);
      add(btn6);
      btn7.setBounds(240,200,50,30);
      add(btn7);
      btn8.setBounds(310,200,50,30);
      add(btn8);
      btn9.setBounds(100,250,50,30);
      add(btn9);
      btn10.setBounds(170,250,50,30);
      add(btn10);
      btn11.setBounds(240,250,50,30);
      add(btn11);
      btn12.setBounds(310,250,50,30);
      add(btn12);
      btn13.setBounds(100,300,50,30);
      add(btn13);
      btn14.setBounds(170,300,50,30);
      add(btn14);
      btn15.setBounds(240,300,50,30);
      add(btn15);
      btn16.setBounds(310,300,50,30);
      add(btn16);
      btn17.setBounds(100,350,80,30);
      add(btn17);
      btn18.setBounds(200,350,50,30);
      add(btn18);
      btn19.setBounds(270,350,50,30);
      add(btn19);

      btn1. addActionListener(this);
      btn2. addActionListener(this);
      btn3. addActionListener(this);
      btn4. addActionListener(this);
      btn5. addActionListener(this);
      btn6. addActionListener(this);
      btn7. addActionListener(this);
      btn8. addActionListener(this);
      btn9. addActionListener(this);
      btn10. addActionListener(this);
      btn11. addActionListener(this);
      btn12. addActionListener(this);
      btn13. addActionListener(this);
      btn14. addActionListener(this);
      btn15. addActionListener(this);
      btn16. addActionListener(this);
      btn17. addActionListener(this);
      btn18. addActionListener(this);
      btn19. addActionListener(this);
    }
   
      public void actionPerformed(ActionEvent ae)
       {
       
       
            
           
       
       
        if(ae. getSource()==btn1)
          {
            txt.setText(txt.getText()+"1");
        }

        if(ae. getSource()==btn2)
        {
            txt.setText(txt.getText()+"2");
        }

        if(ae. getSource()==btn3)
        {
            txt.setText(txt.getText()+"3");
        }
       
        if(ae. getSource()==btn4)
        {
            txt.setText(txt.getText()+"4");
        }
            if(ae. getSource()==btn5)
          {
            txt.setText(txt.getText()+"5");
        }
        if(ae. getSource()==btn6)
          {
            txt.setText(txt.getText()+"6");
        }
        if(ae. getSource()==btn7)
          {
            txt.setText(txt.getText()+"7");
        }
        if(ae. getSource()==btn8)
          {
            txt.setText(txt.getText()+"8");
        }
        if(ae. getSource()==btn9)
          {
            txt.setText(txt.getText()+"9");
        }
        if(ae. getSource()==btn10)
          {
            txt.setText(txt.getText()+"0");
        }
        if(ae. getSource()==btn15)
          {
            txt.setText(txt.getText()+".");
        }
       
       
        if(ae. getSource()==btn11)
          {
            s1=txt.getText();
                        txt.setText("");
            t=1;
        }
        if(ae. getSource()==btn12)
          {
            s1=txt.getText();
                        txt.setText("");
            t=2;
        }
        if(ae. getSource()==btn13)
          {
            s1=txt.getText();
                        txt.setText("");
            t=3;
        }
        if(ae. getSource()==btn14)
          {
            s1=txt.getText();
                        txt.setText("");
            t=4;
        }
       
       
       
        if(ae. getSource()==btn17)
          {
            s1=txt.getText();
            d=s1.length();
            s2=s1.substring(0,d-1);
            txt.setText(String.valueOf(s2));
            t=7;
        }
        if(ae. getSource()==btn18)
          {
            s1=txt.getText();
                        txt.setText("");
            t=8;
        }
        if(ae. getSource()==btn19)
          {
            s1=txt.getText();
                        txt.setText("");
            t=9;
        }
       

        if(ae. getSource()==btn16)
        {
            if(t==1)
            {
              s2=txt.getText();
              n1=Double.parseDouble(s1)+Double.parseDouble(s2);
             
              txt.setText(String.valueOf(n1));
                      }
     
       
            if(t==2)
            {
              s3=txt.getText();
              n1=Double.parseDouble(s1)-Double.parseDouble(s3);
             
                  txt.setText(String.valueOf(n1));
                        }
       
            if(t==3)
            {
              s4=txt.getText();
              n1=Double.parseDouble(s1)*Double.parseDouble(s4);
             
              txt.setText(String.valueOf(n1));
                    }
           
       
            if(t==4)
            {
              s5=txt.getText();
              n1=Double.parseDouble(s1)/Double.parseDouble(s5);
             
                  txt.setText(String.valueOf(n1));
                        }
     
      }
      }
       
    public static void main (String args[])
         {
           Calculator obj=new Calculator();
           obj.setSize(600,500);
           obj.setVisible(true);
             }
}
        
*/


Comments

Popular posts from this blog

Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. In addition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information about the book). You should use this keyword in member methods and constructor. Write a test application named BookTest to create an array of object for 30 elements for class Book to demonstrate the class Book's capabilities.

Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. In addition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information about the book). You should use this keyword in member methods and constructor. Write a test application named BookTest to create an array of object for 30 elements for class Book to demonstrate the class Book's capabilities. import java.io.*; class Book {     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));     String book_name;     int isbn;     String author;     String publisher; ...

Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables‐a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named get Invoice Amount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities.

Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables‐a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named get Invoice Amount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities. import java.io.*; class Invoice {     BufferedReader br = new BufferedReader(new InputStreamReader(System.i...

Create a class called Employee that includes three pieces of information as instance variables—a first name (type String), a last name (type String) and a monthly salary (double). Your class should have a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0. Write a test application named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again

Create a class called Employee that includes three pieces of information as instance variables—a first name (type String), a last name (type String) and a monthly salary (double). Your class should have a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0. Write a test application named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again . import java.io.*; class Employee {     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));     String f_name;     String l_name;     int sal;     Employee()     {         f_name="sarvajeet";     ...