[go: up one dir, main page]

0% found this document useful (0 votes)
25 views19 pages

Calculator

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

Calculator

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

import javax.swing.

*;
import java.awt.*;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;

public class Calculator extends


JFrame implements
ActionListener {
private JTextField display;
private JButton[]
numberButtons;
private JButton[]
functionButtons;
private JButton addButton,
subButton, mulButton, divButton;
private JButton decButton,
equButton, delButton, clrButton,
onOffButton;
private JPanel panel;

private Font myFont = new


Font("Ink Free", Font.BOLD, 30);

double num1 = 0, num2 = 0,


result = 0;
char operator;
public Calculator() {
setTitle("Calculator");
setSize(420, 550);
setDefaultCloseOperation(JFram
e.EXIT_ON_CLOSE);
setLayout(null);

display = new JTextField();


display.setBounds(50, 25,
300, 50);
display.setFont(myFont);
display.setEditable(false);
add(display);

addButton = new
JButton("+");
subButton = new
JButton("-");
mulButton = new
JButton("X");
divButton = new
JButton("/");
decButton = new
JButton(".");
equButton = new
JButton("=");
delButton = new
JButton("Del");
clrButton = new
JButton("C");
onOffButton = new
JButton("ON/OFF");

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

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


functionButtons[i].addActionListe
ner(this);
functionButtons[i].setFont(myFon
t);
functionButtons[i].setFocusable(f
alse);
}

numberButtons = new
JButton[10];
for (int i = 0; i < 10; i++) {
numberButtons[i] = new
JButton(String.valueOf(i));
numberButtons[i].addActionListe
ner(this);
numberButtons[i].setFont(myFon
t);
numberButtons[i].setFocusable(f
alse);
}

delButton.setBounds(50,
430, 145, 50);
clrButton.setBounds(205,
430, 145, 50);
onOffButton.setBounds(50,
490, 300, 50);

add(delButton);
add(clrButton);
add(onOffButton);

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);

add(panel);

setVisible(true);
}

public static void main(String[]


args) {
new Calculator();
}

@Override
public void
actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() ==
numberButtons[i]) {
display.setText(display.getText().
concat(String.valueOf(i)));
}
}
if (e.getSource() ==
decButton) {
display.setText(display.getText().
concat("."));
}
if (e.getSource() ==
addButton) {
num1 =
Double.parseDouble(display.getT
ext());
operator = '+';
display.setText("");
}
if (e.getSource() ==
subButton) {
num1 =
Double.parseDouble(display.getT
ext());
operator = '-';
display.setText("");
}
if (e.getSource() ==
mulButton) {
num1 =
Double.parseDouble(display.getT
ext());
operator = 'X';
display.setText("");
}
if (e.getSource() ==
divButton) {
num1 =
Double.parseDouble(display.getT
ext());
operator = '/';
display.setText("");
}
if (e.getSource() ==
equButton) {
num2 =
Double.parseDouble(display.getT
ext());
switch (operator) {
case '+':
result = num1 +
num2;
break;
case '-':
result = num1 -
num2;
break;
case 'X':
result = num1 *
num2;
break;
case '/':
result = num1 /
num2;
break;
}
display.setText(String.valueOf(re
sult));
num1 = result;
}
if (e.getSource() ==
clrButton) {
display.setText("");
}
if (e.getSource() ==
delButton) {
String string =
display.getText();
display.setText("");
for (int i = 0; i <
string.length() - 1; i++) {
display.setText(display.getText()
+ string.charAt(i));
}
}
if (e.getSource() ==
onOffButton) {
if
(onOffButton.getText().equals("O
N")) {
onOffButton.setText("OFF");
display.setText("");
display.setEditable(false);
} else {
onOffButton.setText("ON");
display.setEditable(true);
}
}
}
}

You might also like