Name: Anonymous 2006-05-01 4:41
hey everyone, i got a problem with java programming. i have a simple jframe with a rectangle on it, then a second jframe with 5 buttons that allow me to move the rectangle around and change the color of it in the other frame. so far so good, all works fine. now i need to be able to control the rectangle with directional-buttons on the keyboard. i got a keylistener and added it to the frame with the square ( square.addkeylistener() < square is the framename) and i tried to handle the event of down-key typed.
public void processKeyEvent(KeyEvent e){
if (e.getID() == KeyEvent.KEY_TYPED) &&
(e.getKeyCode() == KeyEvent.VK_DOWN)){
[code to move rectangle just like with the button pressed, should work]
}
}
now, with the added keylistener
square.addKeyListener(this);
i should be able to handle events, but just like with the button, i'm guessing i need a command setter
up = new JButton("Up");
up.addActionListener(this); //actionlistener added
up.setActionCommad("Up");
but i don't know and i couldn't find anything in the java api =( maybe you can help ! thx in advance
public void processKeyEvent(KeyEvent e){
if (e.getID() == KeyEvent.KEY_TYPED) &&
(e.getKeyCode() == KeyEvent.VK_DOWN)){
[code to move rectangle just like with the button pressed, should work]
}
}
now, with the added keylistener
square.addKeyListener(this);
i should be able to handle events, but just like with the button, i'm guessing i need a command setter
up = new JButton("Up");
up.addActionListener(this); //actionlistener added
up.setActionCommad("Up");
but i don't know and i couldn't find anything in the java api =( maybe you can help ! thx in advance