0% found this document useful (0 votes)
2 views5 pages

Java Swing Calculator Example

The document outlines the implementation of a simple calculator using Java Swing. It includes the creation of a graphical user interface with buttons for digits, operations, and a display field. The calculator performs basic arithmetic operations and handles user input through action listeners.

Uploaded by

quetiepie7
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)
2 views5 pages

Java Swing Calculator Example

The document outlines the implementation of a simple calculator using Java Swing. It includes the creation of a graphical user interface with buttons for digits, operations, and a display field. The calculator performs basic arithmetic operations and handles user input through action listeners.

Uploaded by

quetiepie7
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

OOP with Java Lab /21CSL35

12. B. Develop a simple calculator using Swings.

import [Link].*;
import [Link].*;
import [Link];
import [Link];

class Calculator implements ActionListener


{
JFrame frame; //Creating object of JFrame class
JTextField t;
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bdot,badd,bmul,bsub,bdiv,beq,bclr;

static double a=0,b=0,res=0;


static int op=0;

public void Display()


{
frame=new JFrame();
[Link]("IUK Calculator"); //Title of the JFrame
[Link](225,300); //Calculator Size
[Link](null); //Setting Layout
[Link]([Link]); //Setting Background Color
[Link](true); //window resizing
[Link](true); //Setting window's visibility
[Link](JFrame.EXIT_ON_CLOSE);//Setting default close
operation

t=new JTextField();
[Link](30,10,165,35);

b0=new JButton("0");
[Link](30,50,45,40);
b1=new JButton("1");
[Link](70,50,45,40);
b2=new JButton("2");
[Link](110,50,45,40);
b3=new JButton("3");
[Link](150,50,45,40);

b4=new JButton("4");
[Link](30,90,45,40);
b5=new JButton("5");
[Link](70,90,45,40);
b6=new JButton("6");
[Link](110,90,45,40);
b7=new JButton("7");
[Link](150,90,45,40);

b8=new JButton("8");

Prof. Imran Ulla Khan, Dept. of CSE,SKIT 2022-23


OOP with Java Lab /21CSL35

[Link](30,130,45,40);
b9=new JButton("9");
[Link](70,130,45,40);
bdot=new JButton(".");
[Link](110,130,45,40);
badd=new JButton("+");
[Link](150,130,45,40);

bsub=new JButton("-");
[Link](30,170,45,40);
bmul=new JButton("*");
[Link](70,170,45,40);
bdiv=new JButton("/");
[Link](110,170,45,40);
beq=new JButton("=");
[Link](150,170,45,40);

bclr=new JButton("CLR");
[Link](30,210,165,40);

[Link](t);
[Link](b0);
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](b7);
[Link](b8);
[Link](b9);
[Link](bdot);
[Link](badd);
[Link](bsub);
[Link](bmul);
[Link](bdiv);
[Link](beq);
[Link](bclr);

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

Prof. Imran Ulla Khan, Dept. of CSE,SKIT 2022-23


OOP with Java Lab /21CSL35

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

}
public void actionPerformed(ActionEvent e)
{
if([Link]()==bclr)
{
[Link]("");
}
if([Link]()==b0)
{
[Link]([Link]().concat("0"));
}
if([Link]()==b1)
{
[Link]([Link]().concat("1"));
}
if([Link]()==b2)
{
[Link]([Link]().concat("2"));
}
if([Link]()==b3)
{
[Link]([Link]().concat("3"));
}
if([Link]()==b4)
{
[Link]([Link]().concat("4"));
}
if([Link]()==b5)
{
[Link]([Link]().concat("5"));
}
if([Link]()==b6)
{
[Link]([Link]().concat("6"));
}
if([Link]()==b7)
{
[Link]([Link]().concat("7"));
}
if([Link]()==b8)
{
[Link]([Link]().concat("8"));
}
if([Link]()==b9)
{

Prof. Imran Ulla Khan, Dept. of CSE,SKIT 2022-23


OOP with Java Lab /21CSL35

[Link]([Link]().concat("9"));
}
if([Link]()==bdot)
{
[Link]([Link]().concat("."));
}
if([Link]()==badd)
{
a=[Link]([Link]());
op=1;
[Link]("");
}
if([Link]()==bsub)
{
a=[Link]([Link]());
op=2;
[Link]("");
}
if([Link]()==bmul)
{
a=[Link]([Link]());
op=3;
[Link]("");
}
if([Link]()==bdiv)
{
a=[Link]([Link]());
op=4;
[Link]("");
}
if([Link]()==beq)
{
b=[Link]([Link]());
switch(op)
{
case 1:res=a+b;
break;
case 2:res=a-b;
break;
case 3:res=a*b;
break;
case 4:res=a/b;
break;
}
[Link](""+res);
}
}
}

public class CalculatorP


{

Prof. Imran Ulla Khan, Dept. of CSE,SKIT 2022-23


OOP with Java Lab /21CSL35

public static void main(String[] args)


{
Calculator obj=new Calculator();
[Link]();
}
}

OUTPUT:

Prof. Imran Ulla Khan, Dept. of CSE,SKIT 2022-23

You might also like