Re: KeyListener won't listen
Does the panel with the listener have the focus?
Move your debugging print statement outside of the if clause to see if the method is being called.
What key is tested for here? Why not use the VK_... variable vs the 38?
if(e.getKeyCode() == 38)
Re: KeyListener won't listen
Re: KeyListener won't listen
I tried using the .setFocusable(true) in "public Spelplan". I also kept the printline "Pressed" inside the if-statement. Now it says "Pressed", but the component does not move.
Re: KeyListener won't listen
Quote:
but the component does not move.
What will make it move? What statement is supposed to make it move?
Re: KeyListener won't listen
Quote:
Originally Posted by
Norm
What will make it move? What statement is supposed to make it move?
Because I change the value of y:
Or?? wouldn't that work?
Re: KeyListener won't listen
What difference does changing the value of y make to where the component is?
Re: KeyListener won't listen
Quote:
Originally Posted by
Norm
What difference does changing the value of y make to where the component is?
Because the Y value is the components Y - value. When I change the Y value the component is changing it's position. Then I just thought it sounded reasonable that it would also change position if I made the value change by pressing the "up" - key. It did not do that :(
Re: KeyListener won't listen
Yeah, :D I made it work!
Code :
if(e.getKeyCode() == 38)
{
y = player1.getY();
System.out.println("Pressed");
// y = 200;
player1.setLocation(x, y -= 10);
repaint();
}