Witshaper is the Professional (Computing Skills, English Learning and Soft Skills) Skill Development Training Center. We provide Professional IT Courses and Soft Skill Training in Dehradun to Students, Employees and organization. Who wish to pursue a career in IT Technology. Witshaper is led by a motivated team of IT experts and Soft Skill Professionals. We provide high quality trainings. Our Emphasis is on giving the practical knowledge to the students, so that they will get to know in depth and never forget what they opt, we provide to the students real learning environment. Witshaper prepares students and professionals to be the part of this growing industry. Be a part of Witshaper and get your dreams successful

Sunday 15 May 2016

Applet program : implement KeyListener and ActionListener

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>
*/

No comments:

Post a Comment