How to build a Calculator using JAVA

Realize a mini project Calculator with JAVA.
To complete this project you will need to Install:
=> JAVA 7, 8, or 9
=> To configure JAVA after having Install
=> To install an IDE: BLUE J or NetBEANS, I recommend BLUE J

The Calculator window will consist of 6 rows and 2 columns of an action bar for clearing entries
First
We are going to import a LIBRARIES:
import javax.swing. *;
import javax.swing.ImageIcon;
import java.util. *;
import java.awt. *;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
Secondly
We will declare the variables:
   // Declaration of variables, Variable Declaration
    JPanel p1; // Main Panel, Main Panel
    JLabel l;
    JButton A, B, C, D, E, F;
JTextField t, t1, t2;
JFrame f;
ImageIcon i;
Image e;

   // Declaration of variables, Variable Declaration

Thirdly
We will write the code used to create the action menu bar:
// JMenu bar
  JMenuBar JMenu = new JMenuBar ();
JMenu m = new JMenu ("Option"); // JMenu
JMenu.add (m);
// Add Item (Element in the meno Class)
JMenuItem erase = new JMenuItem ("Erase");
m.add (erase);


Fourth
We will write the codes that will be used to build the 3 lines of the calculator window, do not forget the application has 6 rows and 2 columns. Those are the codes for the first 3 lines:

  // Add in Jpanel 1 and create Jtext
p1 = new JPanel (); // create Panel1

p1.setLayout (new GridLayout (6,2,10,10)); // 4,2,10,10, // Creation of the structure, the shema of the window


// Adding the first line, the Title plus the fields
l = new JLabel ("Enter the 1st number !!!");
p1.add (l); // add the line in the Main Jpanel
t = new JTextField (20);
p1.add (t); // add the fields in the main Jpanel

// 2nd
l = new JLabel ("Enter the 2nd number !!!");
p1.add (l);
t1 = new JTextField (20);
p1.add (t1);
// 3rd Result
l = new JLabel ("Result:");
p1.add (l);
t2 = new JTextField (20);
t2.setBackground (Color.BLACK);
t2.setForeground (Color.GREEN);
t2.setEnabled (false);

p1.add (t2);
Fifth
we will add the other 3 lines:
// Buton Action
A = new JButton ("Additon");
p1.add (A); // add the 1st button to the main Jpanel
B = new JButton ("Multiplication");
p1.add (B);
C = new JButton ("Division");
p1.add ("SOUTH" C);
D = new JButton ("EXP");
p1.add (D);
E = new JButton ("Power");
p1.add (E);
F = new JButton ("Square root");
p1.add (F);
// Action Button
Sixthly
Now we are going to put actions on the Buttons to make the calculations:
// action Addition
// Manage events on the add button,
A.addActionListener (new ActionListener () {

public void actionPerformed (ActionEvent e) {
try {
 String ad = t.getText ();
 String ad2 = t1.getText ();
 double t = Double.parseDouble (ad);
 double b = Double.parseDouble (ad2);
 String w = "" + (t + b);
t2.setText (w);

}
catch (NumberFormatException er) {}

}
}); // Manage events on the add button,

// Manage events on the Multiplication button,
// Action Multiplication
B.addActionListener (new ActionListener () {

public void actionPerformed (ActionEvent e) {
try {
 String ad = t.getText ();
 String ad2 = t1.getText ();
double t = Double.parseDouble (ad);
double b = Double.parseDouble (ad2);
 String w = "" + (t * b);
t2.setText (w);

}
catch (NumberFormatException er) {}

}
}); // Manage events on the Multiplication button,


// Action Division
// Manage events on the button Division,
C.addActionListener (new ActionListener () {

public void actionPerformed (ActionEvent e) {
try {
 String ad = t.getText ();
 String ad2 = t1.getText ();
double t = Double.parseDouble (ad);
double b = Double.parseDouble (ad2);
 String w = "" + (t / b);
t2.setText (w);

}
catch (NumberFormatException er) {}

}
}) // Event management on the button Division,

// Exponetiel
// Manage events on the Exponential button,
D.addActionListener (new ActionListener () {

public void actionPerformed (ActionEvent e) {
try {
 String ad = t.getText ();
 String ad2 = t1.getText ();
double t = Double.parseDouble (ad);
 String w = "" + Math.exp (t);
t2.setText (w);

}
catch (NumberFormatException er) {}

}
}) // Event management on the Exponential button,


//Power
// Manage events on the Power button,
E.addActionListener (new ActionListener () {

public void actionPerformed (ActionEvent e) {
try {
 String ad = t.getText ();
 String ad2 = t1.getText ();
double t = Double.parseDouble (ad);
double b = Double.parseDouble (ad2);
 String w = "" + Math.pow (t, b);
t2.setText (w);

}
catch (NumberFormatException er) {}

}
}); // Manage events on the Power button,


// Racine Carrer
// Manage the events on the Carrer root button,
F.addActionListener (new ActionListener () {

public void actionPerformed (ActionEvent e) {
try {
 String ad = t.getText ();
double t = Double.parseDouble (ad);
 String w = "" + Math.sqrt (t);
t2.setText (w);

}
catch (NumberFormatException er) {}

}
}); // Racine Carrer
// Manage the events on the Carrer root button,


// Handling the events button that clears the inputs, Button bar which erases the TextField
erase.addActionListener (new ActionListener () {

public void actionPerformed (ActionEvent e) {
try {
t.setText ( "");
t1.setText ( "");
t2.setText ( "");


}
catch (NumberFormatException er) {}

}
}) // Handling the event button that clears inputs, Button bar which erases the TextField

Seventh
We are going to write the code that manages the window. That is to say the responsanble to build the window with the title of the window icon image etc.

//Fenetre
f=new JFrame("Calculatrice JBEJ");
f.setVisible(true);
f.getContentPane().add("Center",p1);//Positionement Au Centre
f.setResizable(false); // Permet de ne pas redimentionner la fenetre
f.setJMenuBar(JMenu);// JMenu bar horizontal
i=new ImageIcon("r.jpg");
e=i.getImage();
p1.setBorder(BorderFactory.createLineBorder(Color.BLACK,4));

l.setForeground(Color.BLUE);
f.setSize(300,250);
f.setLocationRelativeTo(null);

Here is the Complet full Code:
import javax.swing.*;
import javax.swing.ImageIcon;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Additon2
{
    //Declaration des variables, Variable Declaration
   JPanel p1; // Panel Principale, Main Panel
   JLabel l;
   JButton A,B,C,D,E,F;
JTextField t,t1,t2;
JFrame f;
ImageIcon i;
Image e;
  //Declaration des variables, Variable Declaration



   //Constructeur
public Additon2(){
// JMenu bar
 JMenuBar JMenu=new JMenuBar ();
JMenu m= new JMenu ("Option");// JMenu
JMenu.add(m);
// Ajouter Item (Element dans le meno Class)
JMenuItem erase= new JMenuItem ("Erase");
m.add(erase);


 //Ajout dans Le Jpanel 1 et creation de Jtext
p1=new JPanel();// creation du Panel1

p1.setLayout(new GridLayout(6,2,10,10)); //4,2,10,10  , // Creation de la structure, du shema de la fenetre

//Ajout de la 1ere ligne, le Titre plus le champs
l=new JLabel("Entrez le 1er nombre!!! ");
p1.add(l);  // ajout de la ligne dans le Jpanel Principal
t=new JTextField(20);
p1.add(t); // ajout du champs dans le Jpanel Principal

// 2eme
l=new JLabel("Entrez le 2eme nombre!!! ");
p1.add(l);
t1=new JTextField(20);
p1.add(t1);
// 3eme Resultat
l=new JLabel("Resultat: ");
p1.add(l);
t2=new JTextField(20);
t2.setBackground (Color.BLACK);
t2.setForeground (Color.GREEN);
t2.setEnabled(false);
p1.add(t2);

// Action Buton
A=new JButton("Additon");
p1.add(A); // ajout du 1er button dans le Jpanel Principal
B=new JButton("Multiplication");
p1.add(B);
C=new JButton("Division");
p1.add("SOUTH",C);
D=new JButton("EXP");
p1.add(D);
E=new JButton("Puissance");
p1.add(E);
F=new JButton("Square root");
p1.add(F);
// Action Buton

//action Addition
// Gestion des evenments sur le button d'addition,
A.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
try{
 String ad= t.getText();
 String ad2= t1.getText();
 double t=Double.parseDouble(ad);
 double b=Double.parseDouble(ad2);
 String w=" "+(t+b);
t2.setText(w);

}
catch(NumberFormatException er){}

}
});// Gestion des evenments sur le button d'addition,

// Gestion des evenments sur le button d'Multiplication,
//Action Multiplication
B.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
try{
 String ad= t.getText();
 String ad2= t1.getText();
double t=Double.parseDouble(ad);
double b=Double.parseDouble(ad2);
 String w=" "+(t*b);
t2.setText(w);

}
catch(NumberFormatException er){}

}
}); // Gestion des evenments sur le button d'Multiplication,


//Action Division
// Gestion des evenments sur le button Division,
C.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
try{
 String ad= t.getText();
 String ad2= t1.getText();
double t=Double.parseDouble(ad);
double b=Double.parseDouble(ad2);
 String w=" "+(t/b);
t2.setText(w);

}
catch(NumberFormatException er){}

}
});// Gestion des evenments sur le button Division,

//Exponetiel
// Gestion des evenments sur le button Exponentiel,
D.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
try{
 String ad= t.getText();
 String ad2= t1.getText();
double t=Double.parseDouble(ad);
 String w=" "+Math.exp(t);
t2.setText(w);

}
catch(NumberFormatException er){}

}
});// Gestion des evenments sur le button Exponentiel,


//Puissance
// Gestion des evenments sur le button Puissance,
E.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
try{
 String ad= t.getText();
 String ad2= t1.getText();
double t=Double.parseDouble(ad);
double b=Double.parseDouble(ad2);
 String w=" "+Math.pow(t,b);
t2.setText(w);

}
catch(NumberFormatException er){}

}
}); // Gestion des evenments sur le button Puissance,


//Racine Carrer
// Gestion des evenments sur le button racine Carrer,
F.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
try{
 String ad= t.getText();
double t=Double.parseDouble(ad);
 String w=" "+Math.sqrt(t);
t2.setText(w);

}
catch(NumberFormatException er){}

}
}); //Racine Carrer
// Gestion des evenments sur le button racine Carrer,


// Gestion des evenments button qui efface les inputs, Button bar which erase the TextField
erase.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
try{
t.setText("");
t1.setText("");
t2.setText("");


}
catch(NumberFormatException er){}

}
});// Gestion des evenments button qui efface les inputs, Button bar which erase the TextField




//Fenetre
f=new JFrame("Calculatrice JBEJ");
f.setVisible(true);
f.getContentPane().add("Center",p1);//Positionement Au Centre
f.setResizable(false); // Permet de ne pas redimentionner la fenetre
f.setJMenuBar(JMenu);// JMenu bar horizontal
i=new ImageIcon("r.jpg");
e=i.getImage();
p1.setBorder(BorderFactory.createLineBorder(Color.BLACK,4));

l.setForeground(Color.BLUE);
f.setSize(300,250);
f.setLocationRelativeTo(null);







}

 public static void main(String [] args){
new Additon2 ();
}
}

Watch the video here to see how it work: https://www.youtube.com/watch?v=5GuWwK3QpzU


Commentaires

Posts les plus consultés de ce blog

Men kijan pou voye MINUT NATCOM-NATCOM pou ZANMIW an 3 etap

Comment partager des minutes sur natcom

Men kijan pou prete kob, minutes, lajan nan men NATCOM Ayiti