0% found this document useful (0 votes)
33 views7 pages

Calculator

This document describes a simple calculator application built using Java Swing. It includes details on the GUI components like buttons and text fields used to build the calculator interface and accepts numeric input and performs basic arithmetic operations like addition, subtraction, multiplication and division.

Uploaded by

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

Calculator

This document describes a simple calculator application built using Java Swing. It includes details on the GUI components like buttons and text fields used to build the calculator interface and accepts numeric input and performs basic arithmetic operations like addition, subtraction, multiplication and division.

Uploaded by

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

Name : Abdul Basit

Class Roll No: 221152


Subject: BSSE
Section (A)
Assignment: Java (OOP)
Calculator
Following is the simple calculator using GUI (Java)

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

class Calculator implements ActionListener {


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

public Calculator() {
frame = new JFrame("Abdul Basit
Calculator");

frame.setDefaultCloseOperation(JFrame.EXIT_O
N_CLOSE);
frame.setSize(420, 550);
frame.setLayout(null);

textField = new JTextField();


textField.setBounds(10, 10, 400,
40);
textField.setFont(myFont);
textField.setEditable(false);

addButton = new JButton("+");


subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decButton = new JButton(".");
equlButton = 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] = equlButton;
functionButtons[6] = delButton;
functionButtons[7] = clrButton;

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

functionButtons[i].addActionListener(this);

functionButtons[i].setFont(myFont);

functionButtons[i].setFocusPainted(false);

functionButtons[i].setBackground(new
Color(39, 40, 34)); // Dark background color

functionButtons[i].setForeground(Color.WHITE
);
}

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


numberButtons[i] = new
JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);

numberButtons[i].setFont(myFont);

numberButtons[i].setFocusPainted(false);

numberButtons[i].setBackground(new Color(64,
64, 64)); // Dark background color

numberButtons[i].setForeground(Color.WHITE);
}

delButton.setBounds(10, 70, 100,


50);
clrButton.setBounds(110, 70, 100,
50);

panel = new JPanel();


panel.setBounds(10, 130, 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(equlButton);
panel.add(divButton);

frame.add(panel);
frame.add(delButton);
frame.add(clrButton);
frame.add(textField);

frame.getContentPane().setBackground(new
Color(102, 102, 102)); // Dark background
color
frame.setVisible(true);
}

public static void main(String[] args) {


new Calculator();
}

@Override
public void actionPerformed(ActionEvent
e) {
// ... (unchanged)
}
}

You might also like