Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Create an applet with two number textboxes and a sum button

  1. /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  2. Create an applet with a button "SUM" and two text boxes "NUM1" and "NUM2" ;
  3. Let user enter two numbers in textboxes when user press SUM button display
  4. the sum of the numbers on the applet.
  5. -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Button;
  9. import java.awt.Graphics;
  10. import java.awt.TextField;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13.  
  14. /*<applet code = "SumTwoNum" width= 200 height = 200>
  15.  </applet>
  16. */
  17.  
  18. public class SumTwoNum extends Applet implements ActionListener
  19. {
  20.  TextField Num1, Num2;
  21.  Button Sum;
  22.  String total = "";
  23.  
  24.  public void init()
  25.  {
  26.   Num1 = new TextField(10);
  27.   add(Num1);
  28.   Num2 = new TextField(10);
  29.   add(Num2);
  30.   Sum = new Button("SUM");
  31.   add(Sum);
  32.   Sum.addActionListener(this);
  33.  }
  34.  
  35.  public void actionPerformed(ActionEvent e)
  36.  {
  37.   int tot;
  38.   tot = Integer.parseInt(Num1.getText()) + Integer.parseInt(Num2.getText());
  39.   total = " "+ tot;
  40.   repaint();
  41.  }
  42.  
  43.  public void paint(Graphics g)
  44.  {
  45.   g.drawString("Sum of Numbers: " + total, 50, 100);
  46.  }
  47. }
Your rating: None Average: 4 (1 vote)
Share this
Anonymous's picture

Thank nice code..

Thank nice code..

Post new comment

2 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.