bigriggers
07-09-2005, 02:13 PM
Hi folks, as a relative "newbie" to this, I seem to have become slighty stuck on an applet! I need to write a simple applet which provides two TextField objects in which the user can enter positive numbers, one in each; the numbers should be summed and the result displayed in a Label object. The result should not be displayed until both numbers have been entered. The second number should be no longer than 3 digits.
Here is what I have so far:
/*an applet which demonstrates the use of text fields*/
import java.awt.*;
public class SimpleTextField extends java.applet.Applet
{
int num1, num2, num3;
Label l;
TextField tf1, tf2;
public void init()
{
add(new Label("Enter first number"));
tf1 = new TextField(3);
add(tf1);
l = new Label("Enter second number");
add(l);
tf2 = new TextField(3);
add(tf2);
}
public boolean action(Event evt, Object arg)
{
if (evt.target instanceof TextField)
{
if(evt.target == tf1)
{
num1 = tf1.getText();
}
else
{
if (tf2.getText().length() > 3)
{
l.setText("Number too long");
}
else
{
num2 = tf2.getText();
l.setText("valid number");
}
}
repaint();
}
return true;
}
public void paint(Graphics g)
{
g.drawString("The result of your two numbers is: " + num3, 10, 115);
}
}
I'm unsure where I need to put the code that adds the two numbers together.
Any help would be greatly appreciated.
Thanks!
Here is what I have so far:
/*an applet which demonstrates the use of text fields*/
import java.awt.*;
public class SimpleTextField extends java.applet.Applet
{
int num1, num2, num3;
Label l;
TextField tf1, tf2;
public void init()
{
add(new Label("Enter first number"));
tf1 = new TextField(3);
add(tf1);
l = new Label("Enter second number");
add(l);
tf2 = new TextField(3);
add(tf2);
}
public boolean action(Event evt, Object arg)
{
if (evt.target instanceof TextField)
{
if(evt.target == tf1)
{
num1 = tf1.getText();
}
else
{
if (tf2.getText().length() > 3)
{
l.setText("Number too long");
}
else
{
num2 = tf2.getText();
l.setText("valid number");
}
}
repaint();
}
return true;
}
public void paint(Graphics g)
{
g.drawString("The result of your two numbers is: " + num3, 10, 115);
}
}
I'm unsure where I need to put the code that adds the two numbers together.
Any help would be greatly appreciated.
Thanks!