key listener in java swings
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyListenerDemo1 extends JFrame implements KeyListener
{
JLabel l,l1;
JTextArea area;
KeyListenerDemo1()
{
Font fnt=new Font("verdena", Font.ITALIC, 30);
setDefaultCloseOperation(EXIT_ON_CLOSE);
l1=new JLabel("test");
add(l1);
l=new JLabel();
l1.setLayout(new FlowLayout(FlowLayout.LEFT));
l.setBounds(20,50,100,20);
area=new JTextArea();
area.setBounds(20,80,300, 300);
area.addKeyListener(this);
add(l);add(area);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void keyPressed(KeyEvent e)
{
l.setText("Key Pressed");
area.setBackground(Color.red);
}
public void keyReleased(KeyEvent e)
{
l.setText("Key Released");
area.setBackground(Color.white);
}
public void keyTyped(KeyEvent e)
{
l.setText("Key Typing.....");
area.setBackground(Color.yellow);
}
public static void main(String[] args)
{
new KeyListenerDemo1();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyListenerDemo1 extends JFrame implements KeyListener
{
JLabel l,l1;
JTextArea area;
KeyListenerDemo1()
{
Font fnt=new Font("verdena", Font.ITALIC, 30);
setDefaultCloseOperation(EXIT_ON_CLOSE);
l1=new JLabel("test");
add(l1);
l=new JLabel();
l1.setLayout(new FlowLayout(FlowLayout.LEFT));
l.setBounds(20,50,100,20);
area=new JTextArea();
area.setBounds(20,80,300, 300);
area.addKeyListener(this);
add(l);add(area);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void keyPressed(KeyEvent e)
{
l.setText("Key Pressed");
area.setBackground(Color.red);
}
public void keyReleased(KeyEvent e)
{
l.setText("Key Released");
area.setBackground(Color.white);
}
public void keyTyped(KeyEvent e)
{
l.setText("Key Typing.....");
area.setBackground(Color.yellow);
}
public static void main(String[] args)
{
new KeyListenerDemo1();
}
}
0 Comments