开发者

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();

});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜