Applet for add two numbers

Ads
 

Applet for add two numbers

what is the java applet code for add two numbers?

View Answers

April 18, 2011 at 2:35 PM

import java.awt.Graphics;

import javax.swing.*;

public class AddApplet extends JApplet {

int sum;

int x;

int y;

public void init() {

String num1;

String num2;

// read first number from the keyboard

num1 = JOptionPane.showInputDialog("Enter a First number");

// read seond number from the ketboard

num2 = JOptionPane.showInputDialog("Enter a Second number");

x= Integer.parseInt(num1); // comvert the string to an integer

y= Integer.parseInt(num2); // convert the string to an integer

sum = x + y;

}

public void paint(Graphics g) {

//writes the output in the applet starting at position 30, 30

g.drawString("The sum of x ("+x+") +y ("+y+")="+sum, 30,30);

}

}


April 18, 2011 at 3:23 PM

1)AppletSum.java:

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

    public class AppletSum extends Applet implements ActionListener{
      TextField text1,text2,output;
      Label label1,label2,label3;
      Button button;
      public void init(){
        setLayout(null);
        label1 = new Label("Enter Number1: ");
        label1.setBounds(20,20,100,20);
        add(label1);

        text1 = new TextField(5);
        text1.setBounds(150,20,100,20);
        add(text1);

        label2 = new Label("Enter Number2: ");
        label2.setBounds(20,50,100,20);
        add(label2);

        text2 = new TextField(5);
        text2.setBounds(150,50,100,20);
        add(text2);

        label3 = new Label("Sum of Two Numbers: ");
        label3.setBounds(20,80,130,20);
        add(label3);

        output = new TextField(5);
        output.setBounds(150,80,100,20);
        add(output);

        button = new Button("Sum");
        button.setBounds(150,110,100,20);
        add(button);

        button.addActionListener(this);
        }
        public void actionPerformed(ActionEvent ae){
        int num1=Integer.parseInt(text1.getText());
        int num2=Integer.parseInt(text2.getText());
        int sum=num1+num2;
        output.setText(Integer.toString(sum));
        }
    }

2)applet.html:

<HTML>
<BODY>
<APPLET ALIGN="CENTER" CODE="AppletSum.class" width = "700" height = "400"></APPLET>
</BODY>
</HTML>

Ads









Related Tutorials/Questions & Answers:
Applet for add two numbers
Applet for add two numbers  what is the java applet code for add two numbers?   import java.awt.Graphics; import javax.swing.*; public...); add(text2); label3 = new Label("Sum of Two Numbers
How to add two numbers in Java?
How to add two numbers in a Java program? In Java you can easily add two number by using the plus (+) operator. The plus operator will add two... how to sum(add) two numbers in Java. Check more tutorials at: Java
Advertisements
Add two big numbers - Java Beginners
Add two big numbers - Java Beginners  Hi, I am beginner in Java and leaned basic concepts of Java. Now I am trying to find example code for adding big numbers in Java. I need basic Java Beginners example. It should easy
Addition of two numbers
Addition of two numbers  addition of two numbers
Add two number in java
Add two number in java In this section you will learn about how to add two... be added, So two add,  convert these String into numeric type..."); System.out.println("Please Enter two number to add :"); Scanner sin=new
Adding two numbers
Adding two numbers  Accepting value ffrom the keyboard and adding two numbers
how to add to numbers in java
how to add to numbers in java  how to add to numbers in java
Listing all even numbers between two numbers
Listing all even numbers between two numbers  Hi, How to write code to list all the even numbers between two given numbers? Thanks   Hi... the numbers. Check the tutorial Write a program to list all even numbers between two
ModuleNotFoundError: No module named 'add_numbers'
ModuleNotFoundError: No module named 'add_numbers'  Hi, My Python... 'add_numbers' How to remove the ModuleNotFoundError: No module named 'add_numbers' error? Thanks   Hi, In your python environment
adding two numbers with out using any operator
adding two numbers with out using any operator  how to add two numbers with out using any operator   import java.math.*; class AddNumbers { public static void main(String[] args) { BigInteger num1=new
To find first two maximum numbers in an array
To find first two maximum numbers in an array  Java program to find first two maximum numbers in an array,using single loop without sorting array
mysql difference between two numbers
mysql difference between two numbers  How to get total bate difference between two dates for example 1/01/2012 and 1/02/2012 in MYSQL?   ... between two date. The syntax of DATEDIFF is .. SELECT DATEDIFF('2012-01-31 23:59
add tow numbers with out using arthamatic operaters
add tow numbers with out using arthamatic operaters  add tow numbers with out using arthamatic operaters
how to add database in Java Applet
to add database in Java Applet below this code is my applet source code . thank...how to add database in Java Applet  hi every java master or Java... Applet implements ActionListener{ TextField
adding two numbers using bitwise operators
adding two numbers using bitwise operators  adding two integer numbers with using bitwise opeators   Hi Friend, Try the following code: public class AddNumberUsingBitwiseOperators { public static void main(String
Generate random number between two numbers in Scala
Generate random number between two numbers in Scala  Hi, How to Generate random number between two numbers in Scala? Thanks   Hi, Following code can be used for generating random number in scala: val rand = new
Generate random number between two numbers in Scala
Generate random number between two numbers in Scala  Hi, How to Generate random number between two numbers in Scala? Thanks   Hi, Following code can be used for generating random number in scala: val rand = new
Sum of two numbers using command line arguments in Java
sum = a+b; Check complete example code at Add Two Numbers in Java. Thanks...Sum of two numbers using command line arguments in Java  Hi, How...? The calculate the sum of two numbers using command line arguments in Java
Swapping of two numbers in java
Swapping of two numbers in java In this example we are going to describe swapping of two numbers in java  without using the third number in java. We... values from the command prompt. The swapping of two numbers is based on simple
How to add dropdown list in a row of a sort table applet?
How to add dropdown list in a row of a sort table applet?  How to add dropdown list in a row of a sort table applet
Swapping of two numbers without using third variable
Swapping of two numbers without using third variable In this tutorial we will learn about swapping of two number in java without using third variable. This is simple program to swap two value using arithmetic operation Swapping of two
How to add two calendars on the same html page
How to add two calendars on the same html page  I have used the same code as on this site http://www.roseindia.net/javascript/javascript... use single calendar for a single html page.but while implementing two calendars
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
Applet
Applet  Write an applet to display a string in an applet. String should be passed as a parameter to an applet
add
How to add two int numbers in Java Example  How to add two int numbers in Java Example  Here is a java example that accepts two integer from the user and find their sum. import java.util.*; class AddNumbers
how to add new column before two column in sql.
how to add new column before two column in sql.  how to add new column in table before two column in sql.   Syntax of Alter table: ALTER... named 'employee' having three fields id, name and salary. Now we want to add two
how to add new column before two column in sql.
how to add new column before two column in sql.  how to add new column in table before two column in sql.   Syntax of Alter table: ALTER... named 'employee' having three fields id, name and salary. Now we want to add two
how to add new column before two column in sql.
how to add new column before two column in sql.  how to add new column in table before two column in sql.   Syntax of Alter table: ALTER... named 'employee' having three fields id, name and salary. Now we want to add two
how to add new column before two column in sql.
how to add new column before two column in sql.  how to add new column in table before two column in sql.   Syntax of Alter table: ALTER... named 'employee' having three fields id, name and salary. Now we want to add two
Applet
Applet  how to run an applet on a web browser

Ads