Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
- Create an applet with a button "SUM" and two text boxes "NUM1" and "NUM2" ;
- Let user enter two numbers in textboxes when user press SUM button display
- the sum of the numbers on the applet.
- -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
- import java.applet.Applet;
- import java.awt.Button;
- import java.awt.Graphics;
- import java.awt.TextField;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- /*<applet code = "SumTwoNum" width= 200 height = 200>
- </applet>
- */
- public class SumTwoNum extends Applet implements ActionListener
- {
- TextField Num1, Num2;
- Button Sum;
- String total = "";
- public void init()
- {
- Num1 = new TextField(10);
- add(Num1);
- Num2 = new TextField(10);
- add(Num2);
- Sum = new Button("SUM");
- add(Sum);
- Sum.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e)
- {
- int tot;
- tot = Integer.parseInt(Num1.getText()) + Integer.parseInt(Num2.getText());
- total = " "+ tot;
- repaint();
- }
- public void paint(Graphics g)
- {
- g.drawString("Sum of Numbers: " + total, 50, 100);
- }
- }

Thank nice code..
Post new comment