Saturday 12 April 2014

A SIMPLE JAVA GUI CALCULATOR

Below is the source code of a simple Java GUI Calculator that I programmed couple of days ago.
This is the Snapshot of the calculator

The calculator works with two text fields the (i.e for first number and second number), I also used uneditable third text field to display the result. Entering a given numbers in the two text fields and clicking an arithmetic operator button will display the result. 

This is the Source Code: 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Calculator extends JFrame
    public JTextField text = new JTextField(10);
    public JTextField text2 = new JTextField(10);
    public JTextField rslt = new JTextField(10);
    public int num = 11;
    public int number;
    public Calculator()
 {
  
      JFrame cover = new JFrame("My App");
      cover.setLayout(new FlowLayout());
      cover.setSize(260, 200);
      cover.setTitle("Maitalata's Calc.");
      cover.setVisible(true);
      
      cover.add(new JLabel("First Numbber   "));
      cover.add(text);
      cover.add(new JLabel("Second Number"));
      cover.add(text2);
      cover.add(new JLabel("Result"));
      cover.add(rslt);
      rslt.setEditable(false);
      JButton b = new JButton("/");
      cover.add(b);
      b.addActionListener( new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e){
          String first = text.getText();
          String second = text2.getText();
           first.trim();
          second.trim();
          int a;
          int b;
          int c;
          a = Integer.parseInt(first);
          b = Integer.parseInt(second);
          c = a / b;
          rslt.setText(c+"");
      }
      } ); 
      JButton c = new JButton("+");
      cover.add(c);
      c.addActionListener( new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e){
          String first = text.getText();
          String second = text2.getText();
          first.trim();
          second.trim();
          int a;
          int b;
          int c;
          a = Integer.parseInt(first);
          b = Integer.parseInt(second);
          c = a + b;
          rslt.setText(c+"");
      }
      } );
      JButton d = new JButton("-");
      cover.add(d);
      d.addActionListener( new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e){
          String first = text.getText();
          String second = text2.getText();
          first.trim();
          second.trim();
          int a;
          int b;
          int c;
          a = Integer.parseInt(first);
          b = Integer.parseInt(second);
          c = a - b;
          rslt.setText(c+"");
      }
      } );
      
      JButton st = new JButton("x");
      cover.add(st);
      st.addActionListener( new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e){
          String first = text.getText();
          String second = text2.getText();
           first.trim();
          second.trim();
          int a;
          int b;
          int c;
          a = Integer.parseInt(first);
          b = Integer.parseInt(second);
          c = a * b;
          rslt.setText(c+"");
      }
      } ); 
      JButton e = new JButton("Clear");
      cover.add(e);
      e.addActionListener( new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e){
          text.setText(null);
          text2.setText(null);
          rslt.setText(null);
      }
      } );
      
      
 }
 public static void main(String args[])
 {        
   Calculator nw = new Calculator();
 } 
}

No comments:

Post a Comment