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: 

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(); 

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

     t2=new JTextField();

     b1=new JButton("Add");

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

     l3=new JLabel("Result");






 public void actionPerformed(ActionEvent ae)

    Double num1=Double.parseDouble(t1.getText());
    Double num2=Double.parseDouble(t2.getText());
       Double value=num1+num2;
       Double value=num1-num2;
       Double value=num1*num2;
       Double value=num1/num2;


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



                 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