0% found this document useful (0 votes)
23 views3 pages

Calculator

This Java code defines an applet calculator class that extends Applet and implements ActionListener. It contains labels, text fields and buttons to take two numbers as input and perform addition, subtraction, multiplication and division and displays the result.
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)
23 views3 pages

Calculator

This Java code defines an applet calculator class that extends Applet and implements ActionListener. It contains labels, text fields and buttons to take two numbers as input and perform addition, subtraction, multiplication and division and displays the result.
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/ 3

package Pkg1;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

//<applet code="Calculator" width="500" height="500">


public class Calculator extends Applet implements ActionListener
{

int num1, num2;


float result=0.0f;
String str ="Answer : ";
Label l1 = new Label("Number1");
Label l2 = new Label("Number2");

TextField t1 = new TextField();


TextField t2 = new TextField();

Button add = new Button("Addtion");


Button sub = new Button("Substraction");
Button mul = new Button("Multiplcation");
Button div = new Button("Division");
Button clr = new Button("Clear");
public void init()
{
setLayout(null);
setBackground(Color.black);
setForeground(Color.white);
setFont(new Font("Arial", Font.BOLD, 14));

add(l1);
l1.setBounds(30,30,65, 30);

setForeground(Color.black);
add(t1);
t1.setBounds(110,30,100, 30);

setForeground(Color.white);
add(l2);
setForeground(Color.black);
l2.setBounds(30,75,65, 30);
add(t2);
t2.setBounds(110,75,100, 30);

add.setBackground(Color.red);
add(add);
add.setBounds(110, 120, 100,35);

sub.setBackground(Color.green);
add(sub);
sub.setBounds(110, 160, 100,35);

mul.setBackground(Color.blue);
add(mul);
mul.setBounds(110, 200, 100,35);
div.setBackground(Color.cyan);
add(div);
div.setBounds(110, 240, 100,35);

clr.setBackground(Color.orange);
add(clr);
clr.setBounds(110, 280, 100,35);

add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
clr.addActionListener(this);
}

public void paint(Graphics g)


{
setForeground(Color.green);
g.drawString(str+result, 300, 200);
}

public void actionPerformed(ActionEvent e)


{
num1 = Integer.parseInt(t1.getText());
num2 = Integer.parseInt(t2.getText());
if(e.getSource()== add)
{
str = "Addition is ";
result = num1+num2;
repaint();
}

if(e.getSource()== sub)
{
str = "Substraction is ";
result = num1-num2;
repaint();
}

if(e.getSource()== mul)
{
str = "Multiplication is ";
result = num1*num2;
repaint();
}

if(e.getSource()== div)
{
str = "Division is ";
result = (num1/num2);
repaint();
}
if(e.getSource()==clr)
{
t1.setText("");
t2.setText("");
str = "Answer : ";
result = 0;
repaint();
}

}
}

You might also like