import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class KeyLis extends Applet implements KeyListener,ActionListener
{
Label l1,l2;
TextField t1,t2;
Button b1;
public void init()
{
l1=new Label("Name ");
l2=new Label("Age ");
t1=new TextField(15);
t2=new TextField(15);
b1=new Button("Clear");
setBackground(Color.cyan);
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
b1.addActionListener(this);
t1.addKeyListener(this);
t2.addKeyListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
t1.setText("");
t2.setText("");
}
}
public void keyPressed(KeyEvent ke)
{}
public void keyReleased(KeyEvent ke)
{
int ch;
if(ke.getSource()==t1)
{
ch=ke.getKeyChar();
//System.out.println(ch);
if(( ch < 65 || ch >90) &&(ch<97 ||ch >122 )&&(ch!=32))
{
t1.setText("");
showStatus("Only alphabets and spaces allowed ... ");
}
else
{
showStatus("");
}
}
if(ke.getSource()==t2)
{
ch=ke.getKeyChar();
if(ch<48 || ch >57)
{
t2.setText("");
showStatus("Only numbers allowed .. ");
}
else
{
showStatus("");
}
}
}
public void keyTyped(KeyEvent ke)
{}
}
/*
<applet code=KeyLis.java height=200 width=200></applet>
*/
import java.applet.*;
import java.awt.event.*;
public class KeyLis extends Applet implements KeyListener,ActionListener
{
Label l1,l2;
TextField t1,t2;
Button b1;
public void init()
{
l1=new Label("Name ");
l2=new Label("Age ");
t1=new TextField(15);
t2=new TextField(15);
b1=new Button("Clear");
setBackground(Color.cyan);
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
b1.addActionListener(this);
t1.addKeyListener(this);
t2.addKeyListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
t1.setText("");
t2.setText("");
}
}
public void keyPressed(KeyEvent ke)
{}
public void keyReleased(KeyEvent ke)
{
int ch;
if(ke.getSource()==t1)
{
ch=ke.getKeyChar();
//System.out.println(ch);
if(( ch < 65 || ch >90) &&(ch<97 ||ch >122 )&&(ch!=32))
{
t1.setText("");
showStatus("Only alphabets and spaces allowed ... ");
}
else
{
showStatus("");
}
}
if(ke.getSource()==t2)
{
ch=ke.getKeyChar();
if(ch<48 || ch >57)
{
t2.setText("");
showStatus("Only numbers allowed .. ");
}
else
{
showStatus("");
}
}
}
public void keyTyped(KeyEvent ke)
{}
}
/*
<applet code=KeyLis.java height=200 width=200></applet>
*/
No comments:
Post a Comment