Hi, this is the blog for programs in different programming languages designd by me. the all programs are original and not copied from any where.
Friday, May 28, 2010
java - simple calculator for add,sub in applet
/*first program in java, strating with basic applet program "calculator.java" compile it in DOS prompt as"c:\java\jdk1.5\bin\javac calculator.java" */
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class calculator extends Applet implements ActionListener
{
Label l1,l2,l3,l4;
TextField t1,t2;
Button b1,b2,b3,b4;
public void init()
{
b1=new Button("add");
b2=new Button("sub");
b3=new Button("mul");
b4=new Button("div");
l1=new Label("number1");
l2=new Label("number2");
l3=new Label("result");
l4=new Label(" ");
t1=new TextField(22);
t2=new TextField(22);
add(l1);add(t1);add(l2);add(t2);add(l3);add(l4);add(b1);add(b2);
add(b3);add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}//init() closed
public void actionPerformed(ActionEvent ae)
{
String s1=t1.getText();
String s2=t2.getText();
int i=Integer.parseInt(s1);
int j=Integer.parseInt(s2);
String str=ae.getActionCommand();
if(str.equals("add"))
l4.setText(String.valueOf(i+j));
else if(str.equals("sub"))
l4.setText(String.valueOf(i-j));
else if(str.equals("mul"))
l4.setText(String.valueOf(i*j));
else if(str.equals("div"))
l4.setText(String.valueOf(i/j));
repaint();
}//actionPerformed() closed
public void paint(Graphics g)
{g.drawString("creator-amrendra",5,240);}
}
//create a html file in which "calculator.class" is tagged named as "cal.html".
/*(html) (head)
(applet code="calculator.class" height=250 width=300)
(/applet) (/head)(/html) */
//note :- use '>' & '<' instead of ')' & '(' respectively.
//run applet as "appletviewer cal.html"
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment