PROJECT -1
Objective – To make a working Calculator with GUI with all operation working with
Buttons
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Calculator implements ActionListener {
double number, answer;
int calculation;
JFrame frame;
JLabel label = new JLabel();
JTextField textField = new JTextField();
JRadioButton onRadioButton = new JRadioButton("on");
JRadioButton offRadioButton = new JRadioButton("off");
JButton buttonZero = new JButton("0");
JButton buttonOne = new JButton("1");
JButton buttonTwo = new JButton("2");
JButton buttonThree = new JButton("3");
JButton buttonFour = new JButton("4");
JButton buttonFive = new JButton("5");
JButton buttonSix = new JButton("6");
JButton buttonSeven = new JButton("7");
JButton buttonEight = new JButton("8");
JButton buttonNine = new JButton("9");
JButton buttonDot = new JButton(".");
JButton buttonClear = new JButton("C");
JButton buttonDelete = new JButton("DEL");
JButton buttonEqual = new JButton("=");
JButton buttonMul = new JButton("x");
JButton buttonDiv = new JButton("/");
JButton buttonPlus = new JButton("+");
JButton buttonMinus = new JButton("-");
JButton buttonSquare = new JButton("x\u00B2");
JButton buttonReciprocal = new JButton("1/x");
JButton buttonSqrt = new JButton("\u221A");
// Colors for the UI
Color pink = new Color(239, 71, 111);
Color yellow = new Color(255, 209, 102);
Color green = new Color(6, 214, 160);
Color pastel = new Color(7, 59, 76)
Calculator() {
prepareGUI();
addComponents();
addActionEvent();
// Method to prepare the GUI
public void prepareGUI() {
frame = new JFrame();
frame.setTitle("Calculator");
frame.setSize(300, 490);
frame.getContentPane().setLayout(null);
frame.getContentPane().setBackground(yellow);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public void addComponents() {
label.setBounds(250, 0, 50, 50);
label.setForeground(Color.black);
frame.add(label);
textField.setBounds(10, 40, 270, 40);
textField.setFont(new Font("Arial", Font.BOLD, 20));
textField.setEditable(false);
textField.setHorizontalAlignment(SwingConstants.RIGHT);
frame.add(textField);
onRadioButton.setBounds(10, 95, 60, 40);
onRadioButton.setSelected(true);
onRadioButton.setFont(new Font("Arial", Font.BOLD, 14));
onRadioButton.setBackground(yellow);
onRadioButton.setForeground(Color.black);
frame.add(onRadioButton);
offRadioButton.setBounds(10, 120, 60, 40);
offRadioButton.setSelected(false);
offRadioButton.setFont(new Font("Arial", Font.BOLD, 14));
offRadioButton.setBackground(yellow);
offRadioButton.setForeground(Color.black);
frame.add(offRadioButton);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(onRadioButton);
buttonGroup.add(offRadioButton);
buttonSeven.setBounds(10, 230, 60, 40);
buttonSeven.setFont(new Font("Arial", Font.BOLD, 20));
buttonSeven.setBackground(pastel);
buttonSeven.setForeground(Color.white);
frame.add(buttonSeven);
buttonEight.setBounds(80, 230, 60, 40);
buttonEight.setFont(new Font("Arial", Font.BOLD, 20));
buttonEight.setBackground(pastel);
buttonEight.setForeground(Color.white);
frame.add(buttonEight);
buttonNine.setBounds(150, 230, 60, 40);
buttonNine.setFont(new Font("Arial", Font.BOLD, 20));
buttonNine.setBackground(pastel);
buttonNine.setForeground(Color.white);
frame.add(buttonNine);
buttonFour.setBounds(10, 290, 60, 40);
buttonFour.setFont(new Font("Arial", Font.BOLD, 20));
buttonFour.setBackground(pastel);
buttonFour.setForeground(Color.white);
frame.add(buttonFour);
buttonFive.setBounds(80, 290, 60, 40);
buttonFive.setFont(new Font("Arial", Font.BOLD, 20));
buttonFive.setBackground(pastel);
buttonFive.setForeground(Color.white);
frame.add(buttonFive);
buttonSix.setBounds(150, 290, 60, 40);
buttonSix.setFont(new Font("Arial", Font.BOLD, 20));
buttonSix.setBackground(pastel);
buttonSix.setForeground(Color.white);
frame.add(buttonSix);
buttonOne.setBounds(10, 350, 60, 40);
buttonOne.setFont(new Font("Arial", Font.BOLD, 20));
buttonOne.setBackground(pastel);
buttonOne.setForeground(Color.white);
frame.add(buttonOne);
buttonTwo.setBounds(80, 350, 60, 40);
buttonTwo.setFont(new Font("Arial", Font.BOLD, 20));
buttonTwo.setBackground(pastel);
buttonTwo.setForeground(Color.white);
frame.add(buttonTwo);
buttonThree.setBounds(150, 350, 60, 40);
buttonThree.setFont(new Font("Arial", Font.BOLD, 20));
buttonThree.setBackground(pastel);
buttonThree.setForeground(Color.white);
frame.add(buttonThree);
buttonDot.setBounds(150, 410, 60, 40);
buttonDot.setFont(new Font("Arial", Font.BOLD, 20));
buttonDot.setBackground(pastel);
buttonDot.setForeground(Color.white);
frame.add(buttonDot);
buttonZero.setBounds(10, 410, 130, 40);
buttonZero.setFont(new Font("Arial", Font.BOLD, 20));
buttonZero.setBackground(pastel);
buttonZero.setForeground(Color.white);
frame.add(buttonZero);
buttonEqual.setBounds(220, 350, 60, 100);
buttonEqual.setFont(new Font("Arial", Font.BOLD, 20));
buttonEqual.setBackground(green);
frame.add(buttonEqual);
buttonDiv.setBounds(220, 110, 60, 40);
buttonDiv.setFont(new Font("Arial", Font.BOLD, 20));
buttonDiv.setBackground(green);
frame.add(buttonDiv);
buttonSqrt.setBounds(10, 170, 60, 40);
buttonSqrt.setFont(new Font("Arial", Font.BOLD, 18));
buttonSqrt.setBackground(green);
frame.add(buttonSqrt);
buttonMul.setBounds(220, 230, 60, 40);
buttonMul.setFont(new Font("Arial", Font.BOLD, 20));
buttonMul.setBackground(green);
frame.add(buttonMul);
buttonMinus.setBounds(220, 170, 60, 40);
buttonMinus.setFont(new Font("Arial", Font.BOLD, 20));
buttonMinus.setBackground(green);
frame.add(buttonMinus);
buttonPlus.setBounds(220, 290, 60, 40);
buttonPlus.setFont(new Font("Arial", Font.BOLD, 20));
buttonPlus.setBackground(green);
frame.add(buttonPlus);
buttonSquare.setBounds(80, 170, 60, 40);
buttonSquare.setFont(new Font("Arial", Font.BOLD, 20));
buttonSquare.setBackground(green);
frame.add(buttonSquare);
buttonReciprocal.setBounds(150, 170, 60, 40);
buttonReciprocal.setFont(new Font("Arial", Font.BOLD, 15));
buttonReciprocal.setBackground(green);
frame.add(buttonReciprocal);
buttonDelete.setBounds(150, 110, 60, 40);
buttonDelete.setFont(new Font("Arial", Font.BOLD, 12));
buttonDelete.setBackground(pink);
buttonDelete.setForeground(Color.white);
frame.add(buttonDelete);
buttonClear.setBounds(80, 110, 60, 40);
buttonClear.setFont(new Font("Arial", Font.BOLD, 12));
buttonClear.setBackground(pink);
buttonClear.setForeground(Color.white);
frame.add(buttonClear);
public void addActionEvent() {
onRadioButton.addActionListener(this);
offRadioButton.addActionListener(this);
buttonClear.addActionListener(this);
buttonDelete.addActionListener(this);
buttonDiv.addActionListener(this);
buttonSqrt.addActionListener(this);
buttonSquare.addActionListener(this);
buttonReciprocal.addActionListener(this);
buttonMinus.addActionListener(this);
buttonSeven.addActionListener(this);
buttonEight.addActionListener(this);
buttonNine.addActionListener(this);
buttonMul.addActionListener(this);
buttonFour.addActionListener(this);
buttonFive.addActionListener(this);
buttonSix.addActionListener(this);
buttonPlus.addActionListener(this);
buttonOne.addActionListener(this);
buttonTwo.addActionListener(this);
buttonThree.addActionListener(this);
buttonEqual.addActionListener(this);
buttonZero.addActionListener(this);
buttonDot.addActionListener(this);
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == onRadioButton) {
enable();
else if (source == offRadioButton) {
disable();
else if (source == buttonClear) {
label.setText("");
textField.setText("");
else if (source == buttonDelete) {
int length = textField.getText().length(); // Get the length of the text field
int number = length - 1; // Get the number of characters to delete
if (length > 0) { // If the length is greater than 0, delete the last character
StringBuilder back = new StringBuilder(textField.getText());
back.deleteCharAt(number);
textField.setText(back.toString());
if (textField.getText().endsWith("")) { // If the text field is empty, set the label to 0
label.setText("");
} else if (source == buttonZero) { // If the zero button is pressed, add a zero to the text field
if (textField.getText().equals("0")) { // If the text field is 0, do nothing
return;
} else { // If the text field is not empty, add a zero to the text field
textField.setText(textField.getText() + "0");
else if (source == buttonOne) {
textField.setText(textField.getText() + "1");
} else if (source == buttonTwo) {
textField.setText(textField.getText() + "2");
} else if (source == buttonThree) {
textField.setText(textField.getText() + "3");
} else if (source == buttonFour) {
textField.setText(textField.getText() + "4");
} else if (source == buttonFive) {
textField.setText(textField.getText() + "5");
} else if (source == buttonSix) {
textField.setText(textField.getText() + "6");
} else if (source == buttonSeven) {
textField.setText(textField.getText() + "7");
} else if (source == buttonEight) {
textField.setText(textField.getText() + "8");
} else if (source == buttonNine) {
textField.setText(textField.getText() + "9");
} else if (source == buttonDot) {
if (textField.getText().contains(".")) { // If the text field contains a dot, do nothing
return;
} else {
textField.setText(textField.getText() + "."); // If the text field does not contain a dot, add a dot
else if (source == buttonPlus) {
String str = textField.getText();
number = Double.parseDouble(textField.getText());
textField.setText("");
label.setText(str + "+");
calculation = 1;
} else if (source == buttonMinus) {
String str = textField.getText();
number = Double.parseDouble(textField.getText());
textField.setText("");
label.setText(str + "-");
calculation = 2;
} else if (source == buttonMul) {
String str = textField.getText();
number = Double.parseDouble(textField.getText());
textField.setText("");
label.setText(str + "X");
calculation = 3;
} else if (source == buttonDiv) {
String str = textField.getText();
number = Double.parseDouble(textField.getText());
textField.setText("");
label.setText(str + "/");
calculation = 4;
} else if (source == buttonSqrt) {
number = Double.parseDouble(textField.getText());
Double sqrt = Math.sqrt(number);
textField.setText(Double.toString(sqrt));
} else if (source == buttonSquare) {
String str = textField.getText();
number = Double.parseDouble(textField.getText());
double square = Math.pow(number, 2);
String string = Double.toString(square);
if (string.endsWith(".0")) {
textField.setText(string.replace(".0", ""));
} else {
textField.setText(string);
label.setText("(sqr)" + str);
} else if (source == buttonReciprocal) {
number = Double.parseDouble(textField.getText());
double reciprocal = 1 / number;
String string = Double.toString(reciprocal);
if (string.endsWith(".0")) {
textField.setText(string.replace(".0", ""));
} else {
textField.setText(string);
} else if (source == buttonEqual) {
switch (calculation) {
case 1:
answer = number + Double.parseDouble(textField.getText());
if (Double.toString(answer).endsWith(".0")) {
textField.setText(Double.toString(answer).replace(".0", ""));
} else {
textField.setText(Double.toString(answer));
label.setText("");
break;
case 2:
answer = number - Double.parseDouble(textField.getText());
if (Double.toString(answer).endsWith(".0")) {
textField.setText(Double.toString(answer).replace(".0", ""));
} else {
textField.setText(Double.toString(answer));
label.setText("");
break;
case 3:
answer = number * Double.parseDouble(textField.getText());
if (Double.toString(answer).endsWith(".0")) {
textField.setText(Double.toString(answer).replace(".0", ""));
} else {
textField.setText(Double.toString(answer));
}
label.setText("");
break;
case 4:
answer = number / Double.parseDouble(textField.getText());
if (Double.toString(answer).endsWith(".0")) {
textField.setText(Double.toString(answer).replace(".0", ""));
} else {
textField.setText(Double.toString(answer));
label.setText("");
break;
public void enable() {
onRadioButton.setEnabled(false);
offRadioButton.setEnabled(true);
textField.setEnabled(true);
label.setEnabled(true);
buttonClear.setEnabled(true);
buttonDelete.setEnabled(true);
buttonDiv.setEnabled(true);
buttonSqrt.setEnabled(true);
buttonSquare.setEnabled(true);
buttonReciprocal.setEnabled(true);
buttonMinus.setEnabled(true);
buttonSeven.setEnabled(true);
buttonEight.setEnabled(true);
buttonNine.setEnabled(true);
buttonMul.setEnabled(true);
buttonFour.setEnabled(true);
buttonFive.setEnabled(true);
buttonSix.setEnabled(true);
buttonPlus.setEnabled(true);
buttonOne.setEnabled(true);
buttonTwo.setEnabled(true);
buttonThree.setEnabled(true);
buttonEqual.setEnabled(true);
buttonZero.setEnabled(true);
buttonDot.setEnabled(true);
public void disable() {
onRadioButton.setEnabled(true);
offRadioButton.setEnabled(false);
textField.setText("");
label.setText(" ");
buttonClear.setEnabled(false);
buttonDelete.setEnabled(false);
buttonDiv.setEnabled(false);
buttonSqrt.setEnabled(false);
buttonSquare.setEnabled(false);
buttonReciprocal.setEnabled(false);
buttonMinus.setEnabled(false);
buttonSeven.setEnabled(false);
buttonEight.setEnabled(false);
buttonNine.setEnabled(false);
buttonMul.setEnabled(false);
buttonFour.setEnabled(false);
buttonFive.setEnabled(false);
buttonSix.setEnabled(false);
buttonPlus.setEnabled(false);
buttonOne.setEnabled(false);
buttonTwo.setEnabled(false);
buttonThree.setEnabled(false);
buttonEqual.setEnabled(false);
buttonZero.setEnabled(false);
buttonDot.setEnabled(false);
public static void main(String[] args)
new Calculator(); // create a new Calculator object
Output :