0% found this document useful (0 votes)
69 views4 pages

Ayoub

This document defines a Java class called Calculatrice that implements a basic graphical calculator application. The class contains fields to store the calculator frame, text display field, number and function buttons. The constructor initializes these components and adds them to the frame. It also defines action listeners for the buttons. The actionPerformed method handles button clicks by updating the display field and performing calculations based on the operator. When equals is clicked, it evaluates the operation on the stored first number and the number currently in the display.

Uploaded by

Ayoub Sanad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views4 pages

Ayoub

This document defines a Java class called Calculatrice that implements a basic graphical calculator application. The class contains fields to store the calculator frame, text display field, number and function buttons. The constructor initializes these components and adds them to the frame. It also defines action listeners for the buttons. The actionPerformed method handles button clicks by updating the display field and performing calculations based on the operator. When equals is clicked, it evaluates the operation on the stored first number and the number currently in the display.

Uploaded by

Ayoub Sanad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

package calculatrice;

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

public class Calculatrice implements ActionListener{


JFrame frame;
JTextField textfield;
JButton[]numberButtons=new JButton[10];
JButton[]functionButtons=new JButton[8];
JButton addButton,subButton,mulButton,divButton;
JButton decButton,equButton,delButton,clrButton;
JPanel panel;
Font myFont=new Font("Arial",Font.BOLD,30);
double num1=0,num2=0,result=0;
char operator;

//-----------------------CONSTRUCTOR----------------------------------------

Calculatrice()
{
frame = new JFrame("Calculatrice");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420,550);
frame.setLayout(null);

textfield=new JTextField();
textfield.setBounds(50, 25, 300, 50);
textfield.setFont(myFont);
textfield.setEditable(false);

addButton = new JButton("+");


subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decButton = new JButton(".");
equButton = new JButton("=");
delButton = new JButton("Delete");
clrButton = new JButton("Clear");

functionButtons[0]=addButton;
functionButtons[1]=subButton;
functionButtons[2]=mulButton;
functionButtons[3]=divButton;
functionButtons[4]=decButton;
functionButtons[5]=equButton;
functionButtons[6]=delButton;
functionButtons[7]=clrButton;

for(int i=0 ; i<8;i++){

functionButtons[i].addActionListener(this);
functionButtons[i].setFont(myFont);
functionButtons[i].setFocusable(false);

}
for(int i=0 ; i<10;i++){

numberButtons[i]=new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(myFont);
numberButtons[i].setFocusable(false);
}
delButton.setBounds(50, 430, 145, 50);
clrButton.setBounds(205 , 430, 145, 50);

panel=new JPanel();
panel.setBounds(50,100,300,300);
panel.setLayout(new GridLayout(4,4,10,10));

panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);
panel.add(decButton);
panel.add(numberButtons[0]);
panel.add(equButton);
panel.add(divButton);

frame.add(panel);
frame.add(delButton);
frame.add(clrButton);
frame.add(textfield);
frame.setVisible(true);
}

//--------------main--------------------------------------------------------

public static void main(String[] args) {


Calculatrice calc = new Calculatrice();
}
@Override
public void actionPerformed(ActionEvent e)

for(int i=0 ;i<10;i++){

if(e.getSource()==numberButtons[i])
{
textfield.setText(textfield.getText().concat(String.valueOf(i)));

}
}
if(e.getSource()==decButton)
{
textfield.setText(textfield.getText().concat("."));

}
if(e.getSource()==addButton)
{
num1=Double.parseDouble(textfield.getText());
operator='+';
textfield.setText("");

}
if(e.getSource()==subButton)
{
num1=Double.parseDouble(textfield.getText());
operator='-';
textfield.setText("");

}
if(e.getSource()==mulButton)
{
num1=Double.parseDouble(textfield.getText());
operator='*';
textfield.setText("");

}
if(e.getSource()==divButton)
{
num1=Double.parseDouble(textfield.getText());
operator='/';
textfield.setText("");

}
if(e.getSource()==equButton)
{
num2=Double.parseDouble(textfield.getText());
switch(operator){
case'+' ->
{
result=num1+num2;
break;
}
case'-' ->
{
result=num1-num2;
break;
}
case'*' ->
{
result=num1*num2;
break;
}
case'/' ->
{
result=num1/num2;
break;
}

}
textfield.setText(String.valueOf(result));
num1=result;

}
if(e.getSource()==clrButton)
{
textfield.setText("");
}
if(e.getSource()==delButton)
{

String string = textfield.getText();


textfield.setText("");
for(int i=0;i<string.length()-1;i++)
{
textfield.setText(textfield.getText()+string.charAt(i));

}
}

You might also like