import java.applet.*;
import java.awt.*;
public class SumProduct extends Applet {
public void init() {
TextField txt1 = new TextField("");
TextField txt2 = new TextField("");
//TextField txt3= new TextField();
Button p = new Button("Sum");
Button q = new Button("Product");
add(p);
add(q);
add(txt1);
add(txt2);
//add(txt3);
}
public Boolean action(Event e, Object args) {
if (e.target == p) {
r = a + b;
}
if (e.target == q) {
r = a * b;
}
}
public void print(Graphics g) {
int a = 0, b = 0, r;
String m;
String s1 = txt1.getText();
String s2 = txt2.getText();
a = Integer.parseInt(s1);
b = Integer.parseInt(s2);
m = String.valueOf(r);
g.drawString(m, 100, 75);
}
}
Asked
Active
Viewed 51 times
0

Scary Wombat
- 44,617
- 6
- 35
- 64

Saadul
- 1
- 3
-
Add more information. What are the errors you get? – Alex Oct 03 '14 at 06:37
-
1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. 3) What is your question? (And no, adding a '?' to the title does not make it a question.) – Andrew Thompson Oct 04 '14 at 02:08
1 Answers
0
You have defined r
in print
but try to use it in action
This code will not compile.
Try moving r
to be a field variable.

Scary Wombat
- 44,617
- 6
- 35
- 64