[go: up one dir, main page]

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

a

abc

Uploaded by

Sonali saha
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)
40 views4 pages

a

abc

Uploaded by

Sonali saha
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

import java.awt.

*;
import java.awt.event.*;

public class CalculatorAWT implements ActionListener {

// Frame and components


Frame frame;
TextField textfield;
Button[] numberButtons = new Button[10];
Button[] functionButtons = new Button[8];
Button addButton, subButton, mulButton, divButton;
Button decButton, equButton, delButton, clrButton;
Panel panel;

// Font for buttons


Font myFont = new Font("Ink Free", Font.BOLD, 30);

// Calculator variables
double num1 = 0, num2 = 0, result = 0;
char operator;

CalculatorAWT() {

// Frame setup
frame = new Frame("Calculator");
frame.setSize(420, 550);
frame.setLayout(null);
frame.setVisible(true);
frame.setResizable(false);
frame.setBackground(Color.PINK); // Set the background to pink

// Textfield setup
textfield = new TextField();
textfield.setBounds(50, 25, 300, 50);
textfield.setFont(myFont);
textfield.setEditable(false);

// Function buttons
addButton = new Button("+");
subButton = new Button("-");
mulButton = new Button("*");
divButton = new Button("/");
decButton = new Button(".");
equButton = new Button("=");
delButton = new Button("Del");
clrButton = new Button("Clr");

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

// Add action listeners to function buttons


for (int i = 0; i < 8; i++) {
functionButtons[i].addActionListener(this);
functionButtons[i].setFont(myFont);
}

// Number buttons (0-9)


for (int i = 0; i < 10; i++) {
numberButtons[i] = new Button(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(myFont);
}

// Button positions
delButton.setBounds(150, 430, 100, 50);
clrButton.setBounds(250, 430, 100, 50);

// Panel for the calculator layout


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

// Add buttons to the panel in a grid layout


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 components to the frame


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

public static void main(String[] args) {


new CalculatorAWT();
}

@Override
public void actionPerformed(ActionEvent e) {

// Handle number button clicks


for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
textfield.setText(textfield.getText().concat(String.valueOf(i)));
}
}

// Handle decimal point button click


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

// Handle operator buttons


if (e.getSource() == addButton) {
num1 = Double.parseDouble(textfield.getText());
operator = '+';
textfield.setText(textfield.getText() + " + ");
}
if (e.getSource() == subButton) {
num1 = Double.parseDouble(textfield.getText());
operator = '-';
textfield.setText(textfield.getText() + " - ");
}
if (e.getSource() == mulButton) {
num1 = Double.parseDouble(textfield.getText());
operator = '*';
textfield.setText(textfield.getText() + " * ");
}
if (e.getSource() == divButton) {
num1 = Double.parseDouble(textfield.getText());
operator = '/';
textfield.setText(textfield.getText() + " / ");
}

// Handle equals button click


if (e.getSource() == equButton) {
String currentText = textfield.getText();
String[] parts = currentText.split(" ");
num2 = Double.parseDouble(parts[parts.length - 1]);

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

// Handle clear button click


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

// Handle delete button click


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