Tuesday, 22 August 2017

Java Swing Calculator Program

                     In this post, we will see Java Swing program for calculator.
                    To know what is Swing, how Swing is different from Applet, how to create Swing Application; check this post http://www.comrevo.com/2017/08/swing-in-java-with-example.html                    

                    To know how to generate event in Java Swing along with Java Swing program for addition of two numbers; check this post http://www.comrevo.com/2017/08/java-swing-program-for-addition-of-two-numbers.html 

  
                    Here, we will see Java Swing program for Calculator.  Go through the following example: 
                                
SwingCalculator.java 

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class SwingCalculator

{
   public static void main(String args[])
     {
       Abc obj=new Abc();
     }
}

class Abc extends JFrame implements ActionListener

{
  JLabel l1,l2,l3;
  JTextField t1,t2;
  JButton b1,b2,b3,b4;
  
  public Abc()
   {
     setLayout(new FlowLayout());

     l1=new JLabel("First Number:");

     t1=new JTextField(); 
     t1.setColumns(20);

     l2=new JLabel("Second Number:");

     t2=new JTextField();
     t2.setColumns(20);

     b1=new JButton("Add");

     b2=new JButton("Sub");
     b3=new JButton("Mul");
     b4=new JButton("Div");

     l3=new JLabel("Result");

     
     add(l1);
     add(t1);
     add(l2);
     add(t2);
     add(b1);
     add(b2);
     add(b3);
     add(b4);
     add(l3);

     b1.addActionListener(this);

     b2.addActionListener(this);
     b3.addActionListener(this);
     b4.addActionListener(this);
          
     setVisible(true);
     setSize(250,400);

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

 public void actionPerformed(ActionEvent ae)

  {
    Double num1=Double.parseDouble(t1.getText());
    Double num2=Double.parseDouble(t2.getText());
    if(ae.getSource()==b1)
     {
       Double value=num1+num2;
       l3.setText(""+value);
     }
    if(ae.getSource()==b2)
     {
       Double value=num1-num2;
       l3.setText(""+value);
     }
    if(ae.getSource()==b3)
     {
       Double value=num1*num2;
       l3.setText(""+value);
     }
    if(ae.getSource()==b4)
     {
       Double value=num1/num2;
       l3.setText(""+value);
     }
  }

}
  


How To Run:
parag@parag-Inspiron-N4010:~/Desktop/prog$ javac SwingCalculator.java 
parag@parag-Inspiron-N4010:~/Desktop/prog$ java SwingCalculator

Output:
  


                        

                 Check MouseListener example in Java Swing in this post http://www.comrevo.com/2017/08/java-swing-mouselistener-example.html

                 Check Java Swing program for using CheckBox component in this post http://www.comrevo.com/2017/08/java-swing-checkbox-example.html

                 Check Java Swing program for using Radio Buttons in this post http://www.comrevo.com/2017/08/java-swing-radio-button-example.html

                 Check other posts on Java Programming in this link http://www.comrevo.com/2017/08/java-programming.html

No comments:

Post a Comment