hi all
i am facing a problem with the below code
i used keylistener so that when i click letter 'v' the program has to exit.
But the following code is not working.
Help me please..
i think we should add listener to the code but how to add listener in the below code ??

import java.awt.*;
import java.awt.event.*;
import  sun.audio.*;
import java.io.*;
public class Games extends Frame implements KeyListener
{
  int x=250,y=460;
  int points=0;
  boolean istop=false;
Thread t=new Thread();
   Games()
  {
   setVisible(true);
   setSize(500,500);
  }
  public void repeat() 
  {
      
    if(istop)
    {
     y+=1; 
    }
    if(!istop)
    {
      y-=1;
    }
     if(y==30)
       {
        points++;

        
       System.out.println("Points are : "+points );   
       istop=true;    
       }
     if(y==460)
     {
       istop=false;
     }
    try
     {
      Thread.sleep(3);
      
     }
    catch(InterruptedException e)
    {
      repaint(); 
    }
   
  }

public void paint(Graphics g)
{
  while(true)
   {
    
    g.clearRect(0,0,500,500);   
    g.fillOval(x,y,30,30);
   
   
    repeat();  
  }

}


public void keyPressed(KeyEvent ke)
{
  
   
 if(ke.getKeyCode()== KeyEvent.VK_V)
  {
    setVisible(false);
  }
  

}

public void keyTyped(KeyEvent ke)
{
  
 if(ke.getKeyCode()== KeyEvent.VK_V)
  {
   System.exit(0);
  }

}
public void keyReleased(KeyEvent ke)
{
  
  

}
public static void main(String args[])
{
 new Games();
 
}
}

thanks in advance

while(true)

the chance of this expression ever returning false is ... well, zero :)

while(true)

the chance of this expression ever returning false is ... well, zero :)

Yes
But as a keyevent occured the control should be handed over to the function..
Isnt it?

hi all
i am facing a problem with the below code
i used keylistener so that when i click letter 'v' the program has to exit.
But the following code is not working.
Help me please..
i think we should add listener to the code but how to add listener in the below code ??

import java.awt.*;
import java.awt.event.*;
import  sun.audio.*;
import java.io.*;
public class Games extends Frame implements KeyListener
{
  int x=250,y=460;
  int points=0;
  boolean istop=false;
Thread t=new Thread();
   Games()
  {
   setVisible(true);
   setSize(500,500);
  }
  public void repeat() 
  {
      
    if(istop)
    {
     y+=1; 
    }
    if(!istop)
    {
      y-=1;
    }
     if(y==30)
       {
        points++;

        
       System.out.println("Points are : "+points );   
       istop=true;    
       }
     if(y==460)
     {
       istop=false;
     }
    try
     {
      Thread.sleep(3);
      
     }
    catch(InterruptedException e)
    {
      repaint(); 
    }
   
  }

public void paint(Graphics g)
{
  while(true)
   {
    
    g.clearRect(0,0,500,500);   
    g.fillOval(x,y,30,30);
   
   
    repeat();  
  }

}


public void keyPressed(KeyEvent ke)
{
  
   
 if(ke.getKeyCode()== KeyEvent.VK_V)
  {
    setVisible(false);
  }
  

}

public void keyTyped(KeyEvent ke)
{
  
 if(ke.getKeyCode()== KeyEvent.VK_V)
  {
   System.exit(0);
  }

}
public void keyReleased(KeyEvent ke)
{
  
  

}
public static void main(String args[])
{
 new Games();
 
}
}

thanks in advance

check this for some help java docs are the best:http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Yes
But as a keyevent occured the control should be handed over to the function..
Isnt it?

just because you call your method 'keyPressed' doesn't mean they're run when you press a key. you'll have to trigger it.
cOrRuPtG3n3t!x 's link should help you out on that part.

just because you call your method 'keyPressed' doesn't mean they're run when you press a key. you'll have to trigger it.
cOrRuPtG3n3t!x 's link should help you out on that part.

Yes
but when key event is triggered, the code for the triggered event is mentioned....

Yes
but when key event is triggered, the code for the triggered event is mentioned....

true, but without the use of a KeyListener, it's just a plain method.

take a look at this code, which is part of the oracle tutorial about this subject.

@stultuske I think that no, never ..., Swing GUI isn't designated for listening of KeyListener, especially JTextComponents, because required some Woodoo for correct Focus / FocusSubsystem and required override method setFocusable()

use KeyBindings,

1) no required override Focus behavior

2) can possible replace/overrive built-in key_shortcuts from BasicXxxUI

3) with same/similair long of written code


EDIT

and for JTextComponents is there Document/DocumentListener

@stultuske I think that no, never ..., Swing GUI isn't designated for listening of KeyListener, especially JTextComponents, because required some Woodoo for correct Focus / FocusSubsystem and required override method setFocusable()

use KeyBindings,

1) no required override Focus behavior

2) can possible replace/overrive built-in key_shortcuts from BasicXxxUI

3) with same/similair long of written code


EDIT

and for JTextComponents is there Document/DocumentListener

I didnt get u korbel

true, but without the use of a KeyListener, it's just a plain method.

take a look at this code, which is part of the oracle tutorial about this subject.

:-O
I used the keylistener
i saw the code and the only point i found is that there is a seperate function that is called on every function that is to be used in the keylistener

you implemented KeyListener, you didn't really use it.
@mKorbel: you've got me there, but I was merely continuing on cOrRuPtG3n3t!x's post, since, it could also work.

commented: :-) +11

you implemented KeyListener, you didn't really use it.
@mKorbel: you've got me there, but I was merely continuing on cOrRuPtG3n3t!x's post, since, it could also work.

Ok then how should i use it?:?:

check the code to which I provided a link for the
addKeyListener method.

check the code to which I provided a link for the
addKeyListener method.

Why to use actionlistener to perform a keylistener?

cOrRuPtG3n3t!x and mKorbel provided you with information about two alternate ways to tackle this problem.

have you actually tried, step by step, to implement one of these?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.