KeyEvent Handling Problem

Hi

I've wite a coden for handling key event but it is't work properly.The problem is each time I press any key it throws some kind of execption. Well
I don't know, why this happening. Kindly help me in this regard. The code isa shown below

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

public class Screen implements KeyListener
{
private Tray T; // Class for drawing rectangle at specific position
public Screen()
{
Tray T = new Tray(30 , 7);
JFrame f = new JFrame("KeyEventHandling");
Container c = f.getContentPane();
c.add(T);
f.setSize(500 , 500);
f.setVisible(true);
f.addKeyListener(this);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
public static void main(String []args)
{
Screen S = new Screen();

}
public void keyPressed(KeyEvent e)
{
System.out.println("Helloooooo "+e.getKeyText( e.getKeyCode() )); // for debugging purpose
T.setX();
T.repaint();
}
public void keyTyped(KeyEvent e)
{
System.out.println("Helloooooo "+e.getKeyTex(e.getKeyCode() )); // for debugging purpose
T.setX();
T.repaint();
}
public void keyReleased(KeyEvent e)
{
System.out.println("Helloooo "+e.getKeyText( e.getKeyCode() ));
T.setX();
T.repaint();
}
}


whenever I press any key The methods of class Tray Isn't call and execption in thrown on Command Prompt

Comments

  • : Hi
    :
    : I've wite a coden for handling key event but it is't work
    : properly.The problem is each time I press any key it throws some
    : kind of execption. Well
    : I don't know, why this happening. Kindly help me in this regard. The
    : code isa shown below
    :
    : import javax.swing.*;
    : import java.awt.event.*;
    : import java.awt.*;
    :
    : public class Screen implements KeyListener
    : {
    : private Tray T; // Class for drawing rectangle at specific position
    : public Screen()
    : {
    : Tray T = new Tray(30 , 7);
    : JFrame f = new JFrame("KeyEventHandling");
    : Container c = f.getContentPane();
    : c.add(T);
    : f.setSize(500 , 500);
    : f.setVisible(true);
    : f.addKeyListener(this);
    : f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
    : }
    : public static void main(String []args)
    : {
    : Screen S = new Screen();
    :
    : }
    : public void keyPressed(KeyEvent e)
    : {
    : System.out.println("Helloooooo "+e.getKeyText( e.getKeyCode()
    : )); // for debugging purpose
    : T.setX();
    : T.repaint();
    : }
    : public void keyTyped(KeyEvent e)
    : {
    : System.out.println("Helloooooo "+e.getKeyTex(e.getKeyCode() ));
    : // for debugging purpose
    : T.setX();
    : T.repaint();
    : }
    : public void keyReleased(KeyEvent e)
    : {
    : System.out.println("Helloooo "+e.getKeyText( e.getKeyCode() ));
    : T.setX();
    : T.repaint();
    : }
    : }
    :
    :
    : whenever I press any key The methods of class Tray Isn't call and
    : execption in thrown on Command Prompt
    :
    What is the exception you get? And in which situation do you get that exception?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion