Java

Calculator Program in Java Eclipse

This tutorial will teach you how to make a simple calculator using Java Eclipse.

First Step : you must extends the JFrame and implements ActionListener .I have attached the video tutorial below it will help you  to do this  step by step.

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;

public class Cal extends JFrame implements ActionListener  {

 private JFrame frame;
 private JTextField txtnum1;
 private JTextField txtnum2;
 JLabel lblresult;
 JButton btnAdd;
 JButton btnMin;
 JButton btnSub;
 JButton btnDiv;
 private int operation; 
 private JLabel lblResult;

 /**
  * Launch the application.
  */ public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     Cal window = new Cal();
     window.frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 /**
  * Create the application.
  */ public Cal() {
  initialize();
  
  
 }

 /**
  * Initialize the contents of the frame.
  */ private void initialize() {
  frame = new JFrame();
  frame.setBounds(100, 100, 509, 370);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(null);
  
  JLabel lblNewLabel = new JLabel("Calculator");
  lblNewLabel.setFont(new Font("Trajan Pro", Font.BOLD, 35));
  lblNewLabel.setBounds(119, 11, 283, 38);
  frame.getContentPane().add(lblNewLabel);
  
  JPanel panel = new JPanel();
  panel.setBounds(43, 73, 402, 184);
  frame.getContentPane().add(panel);
  panel.setLayout(null);
  
  txtnum1 = new JTextField();
  txtnum1.setFont(new Font("Tahoma", Font.BOLD, 13));
  txtnum1.setBounds(207, 38, 153, 20);
  panel.add(txtnum1);
  txtnum1.setColumns(10);
  
  txtnum2 = new JTextField();
  txtnum2.setFont(new Font("Tahoma", Font.BOLD, 13));
  txtnum2.setBounds(207, 84, 158, 20);
  panel.add(txtnum2);
  txtnum2.setColumns(10);
  
  JLabel lblNewLabel_1 = new JLabel("Number 1");
  lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 14));
  lblNewLabel_1.setBounds(38, 41, 102, 14);
  panel.add(lblNewLabel_1);
  
  JLabel lblNumber = new JLabel("Number 2");
  lblNumber.setFont(new Font("Tahoma", Font.BOLD, 14));
  lblNumber.setBounds(38, 87, 102, 14);
  panel.add(lblNumber);
  
   btnAdd = new JButton("+");
  btnAdd.setBounds(10, 126, 89, 47);
  panel.add(btnAdd);
  
  
   btnMin = new JButton("-");
  btnMin.setBounds(109, 126, 89, 47);
  panel.add(btnMin);
  
  
      btnSub = new JButton("*");
  btnSub.setBounds(207, 126, 89, 47);
  panel.add(btnSub);
  
  
   btnDiv = new JButton("/");
  btnDiv.setBounds(303, 126, 89, 47);
  panel.add(btnDiv);
  
  
  btnAdd.addActionListener(this);
  btnMin.addActionListener(this);
  btnSub.addActionListener(this);
  btnDiv.addActionListener(this); 
  
  
  
   lblresult = new JLabel("New label");
   lblresult.setForeground(Color.RED);
  lblresult.setFont(new Font("Times New Roman", Font.BOLD, 24));
  lblresult.setBounds(260, 276, 120, 28);
  frame.getContentPane().add(lblresult);
  
  lblResult = new JLabel("Result");
  lblResult.setFont(new Font("Times New Roman", Font.BOLD, 22));
  lblResult.setBounds(164, 276, 120, 28);
  frame.getContentPane().add(lblResult);
 }
 
 
 
 public void getTotal(int operation)
  {
  if(txtnum1.getText().length()!=0 && txtnum2.getText().length()!=0)
  {
  switch(operation)
  {
  case 1:
   lblresult.setText(""+(Double.parseDouble(txtnum1.getText()) + Double.parseDouble(txtnum2.getText())));
  break;
  
  case 2:
   lblresult.setText(""+(Double.parseDouble(txtnum1.getText() ) - Double.parseDouble(txtnum2.getText())));
  break;
  
  case 3:
   lblresult.setText(""+(Double.parseDouble(txtnum1.getText()) *  Double.parseDouble(txtnum2.getText())));
  break;
  case 4:
   lblresult.setText(""+(Double.parseDouble(txtnum1.getText()) / Double.parseDouble(txtnum2.getText())));
  break;
  } 
  }
  
  }
 
 public void actionPerformed(ActionEvent ae)
  {
  if(ae.getSource()==btnAdd)
  {
  operation=1;
  getTotal(operation);
  
  }
  else if(ae.getSource()==btnMin)
  {
  operation=2;
  getTotal(operation);
  }
  
  else if(ae.getSource()==btnSub)
  {
  operation=3;
  getTotal(operation);
  }
  
  else if(ae.getSource()==btnDiv)
  {
  operation=4;
  getTotal(operation);
  }
  } 
 
}

 

admin

Recent Posts

Creating Grocery Inventory App Using React

Introduction to Grocery Inventory Apps Managing grocery inventory can be a daunting task, but with…

4 hours ago

Fish Inventory Shop Management System in Angular

This article explain how to make a Fish Inventory Management App in Angular.this app explain…

6 days ago

Fish Inventory Management with React

Introduction to Fish Inventory Management In the aquaculture industry, managing fish inventory is crucial for…

1 week ago

Java GUI CRUD for Beginners

Introduction to Java GUI CRUD Java is a powerful programming language widely used for building…

1 week ago

Creating Beautiful Login Form Design Using React

Introduction to Login Form Design Designing an effective and beautiful login form is crucial for…

2 weeks ago

Creating Responsive Login Form with React

Introduction In today creating a responsive login form is essential for providing a seamless user…

2 weeks ago