Thursday, December 2, 2010

Source code for a simple java programming calculator(Using Applets)

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

public class Calculator extends Applet implements ActionListener
{
String myBuffer;
String firstBuffer;
String secondBuffer;

String operation;

int result;
String passtoresult;

Panel row1 = new Panel();
Panel row2 = new Panel();
Panel row3 = new Panel();

Label titleLabel = new Label("Calculator", Label.CENTER);
TextField resultField = new TextField(15);

Button one = new Button ("1");
Button zero = new Button ("0");
Button two = new Button ("2");
Button three = new Button ("3");
Button four = new Button ("4");
Button five = new Button ("5");
Button six = new Button ("6");
Button seven = new Button ("7");
Button eight = new Button ("8");
Button nine = new Button ("9");
Button add = new Button ("add");
Button subtract = new Button ("subtract");
Button multiply = new Button ("multiply");
Button divide = new Button ("divide");
Button equals = new Button ("equals");

public void init()
{
myBuffer="";
firstBuffer="";
secondBuffer="";

one.addActionListener(this);
zero.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
add.addActionListener(this);
subtract.addActionListener(this);
multiply.addActionListener(this);
divide.addActionListener(this);
equals.addActionListener(this);

setBackground(Color.white);

GridLayout line1 = new GridLayout(2,1,100,10);
row1.setLayout(line1);
row1.add(titleLabel);row1.add(resultField);
add(row1);

GridLayout line2 = new GridLayout(3,4,2,2);
row2.setLayout(line2);
row2.add(one);row2.add(two);
row2.add(three);row2.add(four);row2.add(five);
row2.add(six);row2.add(seven);row2.add(eight);row2.add(nine);
row2.add(zero);
add(row2);

GridLayout line3 = new GridLayout(3,2,2,2);
row3.setLayout(line3);
row3.add(add);
row3.add(subtract);
row3.add(multiply);
row3.add(divide);
row3.add(equals);
add(row3);

}


public void actionPerformed(ActionEvent event)
{
//if (event.getSource() == zero)
//......add code here otherwise your compiler will complain


}
}

No comments:

Post a Comment