Friday 29 March 2013

Calculator Program in Applet

Calculator Program in applet
Similar programs

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

public class MyButtonDialog extends Applet implements ActionListener{
Label x1, x2, x3;

TextField t1, t2, t3;

Button b1, b2, b3, b4, b5,b6;


public void init()
{



setBackground(Color.cyan);
setForeground(Color.blue);
setLayout(new FlowLayout());

x1 = new Label("Number1");
x2 = new Label("Number2");
x3 = new Label("Result ");

t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(10);

b1 = new Button("Add");
b2 = new Button("Sub");
b3 = new Button("Mul");
b4 = new Button("Div");
b5 = new Button("Reset");
b6 = new Button("Close");


add(x1);
add(t1);
add(x2);
add(t2);
add(x3);
add(t3);

add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);

}

public void actionPerformed(ActionEvent ae) {
int a, b, c;

try {
a = Integer.parseInt(t1.getText());
}
catch (NumberFormatException ne)
{
a = 0;
t1.setText("0");
}

try {
b = Integer.parseInt(t2.getText());
}
catch (NumberFormatException ne)
{
b = 0;
t2.setText("0");
}

if (ae.getSource() == b1) {
c = a + b;
t3.setText(String.valueOf(c));
}

if (ae.getSource() == b2) {
c = a - b;
t3.setText(String.valueOf(c));
}

if (ae.getSource() == b3) {
c = a * b;
t3.setText(String.valueOf(c));
}

if (ae.getSource() == b4) {
c = a / b;
t3.setText(String.valueOf(c));
}

if (ae.getSource() == b5) {
t1.setText("");
t2.setText("");
t3.setText("");
}

if (ae.getSource() == b6) {

System.exit(0);
//setVisible(false);
//dispose();

}

}


}

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel