0% found this document useful (0 votes)
1 views3 pages

Import Java

The document contains a Java applet code for a simple calculator that sums two numbers inputted by the user. It includes UI components such as text fields for number input and a button to calculate the sum, displaying the result in a non-editable text field. The applet handles invalid input by showing an error message in the result field.

Uploaded by

Richa Dubey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Import Java

The document contains a Java applet code for a simple calculator that sums two numbers inputted by the user. It includes UI components such as text fields for number input and a button to calculate the sum, displaying the result in a non-editable text field. The applet handles invalid input by showing an error message in the result field.

Uploaded by

Richa Dubey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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?

You might also like