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

Java Swing Calculator Program

This document is a Java program that implements a simple calculator using Swing for the graphical user interface. It allows users to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. The program features buttons for digits and operations, and displays results in a text field.

Uploaded by

227r5a1203
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)
9 views4 pages

Java Swing Calculator Program

This document is a Java program that implements a simple calculator using Swing for the graphical user interface. It allows users to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. The program features buttons for digits and operations, and displays results in a text field.

Uploaded by

227r5a1203
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

// Aim Write a program to implement calculator program

import [Link].*;

import [Link].*;

import [Link].*;

public class Calculator extends JFrame implements ActionListener {

JButton b10, b11, b12, b13, b14, b15;

JButton b[] = new JButton[10];

int i, n1, n2, r;

JTextField res;

char op;

public Calculator() {

setLayout(new BorderLayout());

JPanel p = new JPanel();

[Link](new GridLayout(4, 4));

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

b[i] = new JButton("" + i);

[Link](b[i]);

b[i].addActionListener(this);

b10 = new JButton("+");

[Link](b10);

[Link](this);

b11 = new JButton("-");

[Link](b11);

[Link](this);

b12 = new JButton("*");

[Link](b12);

[Link](this);

b13 = new JButton("/");

[Link](b13);
[Link](this);

b14 = new JButton("=");

[Link](b14);

[Link](this);

b15 = new JButton("C");

[Link](b15);

[Link](this);

res = new JTextField(10);

add(res, [Link]);

add(p, [Link]);

setVisible(true);

setSize(400, 400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public void actionPerformed(ActionEvent ae) {

JButton pb = (JButton) [Link]();

boolean opf = false;

if (pb == b15) { // Clear

n1 = n2 = r = 0;

[Link]("");

} else if (pb == b14) { // Equals

n2 = [Link]([Link]());

eval();

[Link]("" + r);

} else {

if (pb == b10) {

op = '+';

opf = true;

} else if (pb == b11) {

op = '-';
opf = true;

} else if (pb == b12) {

op = '*';

opf = true;

} else if (pb == b13) {

op = '/';

opf = true;

if (!opf) {

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

if (pb == b[i]) {

String t = [Link]();

t = t + i;

[Link](t);

} else {

n1 = [Link]([Link]());

[Link]("");

int eval() {

switch (op) {

case '+':

r = n1 + n2;

break;

case '-':

r = n1 - n2;
break;

case '*':

r = n1 * n2;

break;

case '/':

r = n1 / n2;

break;

return r;

public static void main(String args[]) {

new Calculator();

You might also like