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:
- How to Create Forgot System Password with PHP & MySQL
- Datatables Editable Add Delete with Ajax, PHP & MySQL-My programming school
- Download Login and Registration form in PHP & MySQL-MPS
- Export Data to Excel in Php Code -My programming school
- Hospital Database Management System with PHP MySQL
- Java projects for beginners | java developer software | UML diagram
- Traffic Light using Applet in Java | algorithm and source code
- Currency converter using java
- Java program to design simple calculator with the use of grid layout
- Simple Notepad Editor using java|Gui application
- Online quiz application using java GUI with complete source code
- How to create color changer mini-project using java applet with source code
- How to make a Calculator using java Swing, GUI
- Canteen food order, generate total bill using java swing
- Currency Convertor java project, swing component
- Python Number Guessing Game mini Project
See my more website