
what is the java applet code for add two numbers?

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);
}
}

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>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.