Monday, 17 June 2013

Java program for typing on frame


//WAP to handle Key event

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class key1 extends JFrame implements KeyListener
{
    String msg=" ";
    char ch;

    key1()
    {
        addKeyListener(this);
    }

    public void keyPressed(KeyEvent ke)
    {
        ch=ke.getKeyChar();
        msg=msg+ch;
    }
    public void keyTyped(KeyEvent ke)
    {

    }
    public void keyReleased(KeyEvent ke)
    {
        Graphics g=getGraphics();
        g.drawString(msg,100,100);
    }
    public static void main(String args[])
    {
        key1 k=new key1();
        k.setTitle("Key event");
        k.setSize(400,400);
        k.setVisible(true);
    }
}

No comments: