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

Import Javax

The document is a Java program that implements a simple graphical calculator using Swing. It includes a display field, number buttons, and function buttons for basic arithmetic operations. The calculator processes user input and performs calculations based on the selected operator, displaying the result on the screen.

Uploaded by

mamomohi13
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)
11 views7 pages

Import Javax

The document is a Java program that implements a simple graphical calculator using Swing. It includes a display field, number buttons, and function buttons for basic arithmetic operations. The calculator processes user input and performs calculations based on the selected operator, displaying the result on the screen.

Uploaded by

mamomohi13
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/ 7

import javax.swing.

*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class CalculatorApp extends JFrame implements ActionListener {

// Components

private JTextField display;

private JButton[] numberButtons = new JButton[10];

private JButton[] functionButtons = new JButton[8];

private JButton addButton, subButton, mulButton, divButton;

private JButton decButton, equButton, delButton, clrButton;

private JPanel panel;

// Variables for calculation

private double num1 = 0, num2 = 0, result = 0;

private char operator;

// Constructor

public CalculatorApp() {

// Set up the frame

setTitle("Calculator");

setSize(420, 550);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(null);
// Display field

display = new JTextField();

display.setBounds(50, 25, 300, 50);

display.setEditable(false);

display.setFont(new Font("Arial", Font.PLAIN, 24));

add(display);

// Buttons

addButton = new JButton("+");

subButton = new JButton("-");

mulButton = new JButton("*");

divButton = new JButton("/");

decButton = new JButton(".");

equButton = new JButton("=");

delButton = new JButton("Del");

clrButton = new JButton("C");

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(new Font("Arial", Font.PLAIN, 18));

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(new Font("Arial", Font.PLAIN, 18));

numberButtons[i].setFocusable(false);

// Panel for buttons

panel = new JPanel();

panel.setBounds(50, 100, 300, 300);

panel.setLayout(new GridLayout(4, 4, 10, 10));

// Add buttons to the panel

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 other buttons to the frame

delButton.setBounds(50, 430, 145, 50);

clrButton.setBounds(205, 430, 145, 50);

add(delButton);

add(clrButton);

add(panel);

setVisible(true);

// ActionListener implementation

@Override
public void actionPerformed(ActionEvent e) {

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

if (e.getSource() == numberButtons[i]) {

display.setText(display.getText() + i);

if (e.getSource() == decButton) {

display.setText(display.getText() + ".");

if (e.getSource() == addButton) {

num1 = Double.parseDouble(display.getText());

operator = '+';

display.setText("");

if (e.getSource() == subButton) {

num1 = Double.parseDouble(display.getText());

operator = '-';

display.setText("");

if (e.getSource() == mulButton) {

num1 = Double.parseDouble(display.getText());

operator = '*';

display.setText("");

if (e.getSource() == divButton) {
num1 = Double.parseDouble(display.getText());

operator = '/';

display.setText("");

if (e.getSource() == equButton) {

num2 = Double.parseDouble(display.getText());

switch (operator) {

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;

case '*':

result = num1 * num2;

break;

case '/':

result = num1 / num2;

break;

display.setText(String.valueOf(result));

num1 = result;

if (e.getSource() == clrButton) {

display.setText("");
}

if (e.getSource() == delButton) {

String currentText = display.getText();

if (!currentText.isEmpty()) {

display.setText(currentText.substring(0, currentText.length() - 1));

// Main method

public static void main(String[] args) {

new CalculatorApp();

You might also like