I have created the following program that accepts values and adds them up when you click on the 'add' button. Can you pliz specify the condition to be used such that the user is not allowed to enter an alphabet or other character. if that happens an error message should be displayed on label l4.
import java.awt.*;
import java.awt.event.*;
class Myframe extends Frame implements ActionListener
{
TextField t1,t2,t3;
Button b1,b2;
Label l1,l2,l3,l4;
Myframe()
{
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
b1=new Button("Add");
b2=new Button("Exit");
l1=new Label("Enter num1");
l2=new Label("Enter num2");
l3=new Label("Result");
l4 = new Label(" ");
setLayout(new FlowLayout());
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
add(b2);
add(l4);
b1.addActionListener(this);
b2.addActionListener(this);
setSize(300,300);
setVisible(true);
}
public static void main(
String s[])
{
new Myframe();
}
public void actionPerformed(ActionEvent ae)
{
String str1 = t1.getText();
String str2 = t2.getText();
if(ae.getSource()==b1)
{
if(str1 <='a' || str1 >= 'z')
{
la.setText("Enter a number");
}
else
{
int x=Integer.parseInt(str1);
int y=Integer.parseInt(str2);
t3.setText(String.valueOf(x+y));
}
}
else if(ae.getSource()==b2);
{
System.exit(0);
}
}
}