开发者

KeyListener not reacting

开发者 https://www.devze.com 2023-02-22 05:45 出处:网络
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 :/

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

精彩评论

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