KeyListener not reacting
Im trying to use a KeyListener to do something when the button m is pressed, it needs to add an image to a JPanel, however it doesn't do anything :/
I use :
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()==('m')){
panel.add(mario);
Thread marioS = new AePlayWave("sm64itsamemario.wav");
marioS.start();
}
}
});
Edit:
The answer is to set the focus to the panel before tr开发者_如何学Goying to invoke the listener :)so I added a mouselistener that sets the focus to the panel on click :
panel.addMouseListener( new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
panel.requestFocusInWindow();
});
精彩评论