It doesn't even print the keys to the command prompt. Thanks in advance.
Here's the class for the keylistener
public static class KeyWatch extends KeyAdapter implements KeyListener
{
int x;
int y;
public KeyWatch(int x, int y)
{
x=this.x;
y=this.y;
}
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if(keyCode==KeyEvent.VK_UP)
{
System.out.println("up");
y=y+20;
}
if(keyCode==KeyEvent.VK_DOWN)
{
System.out.println("Down");
y=y-20;
}
if(keyCode==KeyEvent.VK_RIGHT)
{
System.out.println("Right");
x=x+20;
}
if(keyCode==KeyEvent.VK_LEFT)
{
System.out.println("Left");
x=x-20;
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
and here's the call in the window out of context
window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
KeyWatch watcher = new KeyWatch(x, y);
playerlabel = new JLabel(player);
playerlabel.addKeyListener(watcher);
playerlabel.setLocation(x, y);
anyway the point is to move the Jlabel around the window.
Any help is appreciated.
You need to make the label get the focus:
playerLabel.requestFocus();
Call this after the key listener has been added, the label has been added to the view hierarchy and it became visible.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.