import [Link].
Applet;
import [Link].*;
import [Link].*;
/*
<applet code="SumApplet" width=300 height=200>
</applet>
*/
public class SumApplet extends Applet implements ActionListener {
TextField num1Field, num2Field, resultField;
Button sumButton;
public void init() {
// Create UI components
Label l1 = new Label("Enter first number:");
num1Field = new TextField(10);
Label l2 = new Label("Enter second number:");
num2Field = new TextField(10);
sumButton = new Button("Calculate Sum");
[Link](this);
Label l3 = new Label("Sum:");
resultField = new TextField(10);
[Link](false);
// Add components to applet
add(l1);
add(num1Field);
add(l2);
add(num2Field);
add(sumButton);
add(l3);
add(resultField);
@Override
public void actionPerformed(ActionEvent e) {
try {
// Read and parse numbers
double num1 = [Link]([Link]().trim());
double num2 = [Link]([Link]().trim());
// Calculate sum
double sum = num1 + num2;
// Display result
[Link]([Link](sum));
} catch (NumberFormatException ex) {
[Link]("Invalid input");
}
Works with integers and decimals.
If you want, I can also give you a Swing-based version that works in modern Java without
applets.
Do you want me to convert this to a Swing GUI so it runs anywhere?