How to Create a simple calculator using Java Swing | Mini Project – myprogrammingschool

Implement a Simple Calculator using Java Swing:

Swing API is a set of extensible GUI Components to make a developer’s life easier for building JAVA-based front-end / GUI applications. It is built over the AWT API and serves as a replacement for the AWT API as it has almost every control corresponding to the AWT controls. The swing component follows a model-view-controller architecture to meet the following criteria.
 
A single API should suffice for multiple types of look and feel.
The API is modeled so that the highest level of API is not required for the data.
The API is to use the Java Bean model so that builder tools and IDEs can provide better services to developers to use.
 
Here you can do a different arithmetic operation like addition, subtraction, multiplication, and division

Source Code:

import java.awt.*;

import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class Calculater extends Applet implements ActionListener
{
//Frame f;
Frame f=new Frame();
Label l1=new Label("enter 1st number");
Label l2=new Label("enter 2nd number");
Label l3=new Label("Result");
TextField t1=new TextField();
TextField t2=new TextField();
TextField t3=new TextField();
Button b1=new Button("Add");
Button b2=new Button("Sub");
Button b3=new Button("Mult");
Button b4=new Button("div");
Button b5=new Button("cancel");
Calculater()
{
l1.setBounds(50,100,100,20);
l2.setBounds(50,130,100,20);
l3.setBounds(50,170,100,20);
t1.setBounds(200,100,100,20);
t2.setBounds(200,140,100,20);
t3.setBounds(200,180,100,20);
b1.setBounds(10,250,100,20);
b2.setBounds(100,250,100,20);
b3.setBounds(200,250,100,20);
b4.setBounds(300,250,100,20);
b5.setBounds(400,250,100,20);
f.setLayout(null);
f.setVisible(true);
f.setSize(600,400);
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(l3);
f.add(t3); 
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
//f.setlayout(null);
//f.setVisible(true);
//f.setSize(400,350);
}
public void actionPerformed(ActionEvent ee)
{
int n1=Integer.parseInt(t1.getText());
int n2=Integer.parseInt(t2.getText());
if(ee.getSource().equals(b1))
{
t3.setText(String.valueOf(n1+n2));
}

if(ee.getSource().equals(b2))
{
t3.setText(String.valueOf(n1-n2));
}

if(ee.getSource().equals(b3))
{
t3.setText(String.valueOf(n1*n2));
}

if(ee.getSource().equals(b4))
{
t3.setText(String.valueOf(n1/n2));
}
if(ee.getSource().equals(b5))
{
System.exit(0);
}
}
 public static void main(String [] args)
{
new Calculater();
}
}

if you want to see more project click here

COMPLETED PROJECTS:

See my more website

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment