JButton 破坏了我的 Keylistener

1 java swing key-bindings listener

使用 NETBeans 的 Java

你好stackoverflow,我有一个问题需要帮助。简而言之,我在 jpanel 上有一个鼠标侦听器和一个键盘侦听器,一切正常,除非我按下我的一个 jbuttons,然后键盘侦听器开始工作。任何人都可以解释这个问题,是面板现在专注于按钮而不是键盘,我不知所措。

这是代码,如果没有参考,假设它在那里,整个面板代码是 500+ 长,所以我剪了很多。

在此先感谢您的帮助。

package tankgame;

public class TankPanel extends JPanel implements KeyListener, 
MouseListener,MouseMotionListener

{
JButton back,shop, menu, health, speed, rapidfire, shootradius;

TankPanel()
{
    setLayout( null );

    addMouseListener(this);
    addMouseMotionListener(this);

    addKeyListener(this);
    setFocusable(true);


    shop= new JButton("SHOP");
    shop.addMouseListener(this);
    shop.setBounds(400,0, 80,15);
    add(shop);

}

public void keyPressed(KeyEvent k)
{
        char c = k.getKeyChar();

        if(c=='u')
        {
            u++;
            System.out.println(u+" = u");
        }
        if(c=='i')
        {
            i++;
            System.out.println(i+" = i");
        }
        if( c == 'd' )
        {
            if(Ptank.pic==PlayerTankE)
            {
                if(Ptank.move==true)
                {
                    Pbarrel.x+=Ptank.speed;
                    Ptank.x+=Ptank.speed;
                }
            }
            else
            {
                if(Ptank.pic==PlayerTankN || Ptank.pic==PlayerTankS)
                {
                    Ptank.x = Ptank.x - 5;
                    Ptank.y=Ptank.y+5;
                }

                Ptank.setPic(PlayerTankE);
                Ptank.width=35;
                Ptank.height = 23;

            }
        }
        setFocusable(true);
            repaint();
    }

public void keyReleased(KeyEvent k)
{
}

public void keyTyped(KeyEvent k)
{
}



public void mouseClicked(MouseEvent e)
{    
    //Invoked when the mouse button has been clicked (pressed and released)



}
public void mouseEntered(MouseEvent e)
{//Invoked when the mouse enters a component.
}

public void mouseExited(MouseEvent e)
{ //Invoked when the mouse exits a component.
}

public void mousePressed(MouseEvent e)
{//Invoked when a mouse button has been pressed on a component.
    if(e.getSource()==back)
    {
        System.out.println(456);
        System.out.println(back.getLocation().x + " "+back.getLocation().y);
    }

    else if(e.getSource() == menu)
    {
        changebuttons("menu");
        System.out.println(456);
        System.out.println(menu.getLocation().x + " "+menu.getLocation().y);
    }
    else if(e.getSource() == shop)
    {
        changebuttons("shop");
        System.out.println(456);
        System.out.println(shop.getLocation().x + " "+shop.getLocation().y);
    }
    else if(e.getButton() == MouseEvent.BUTTON1)
    {
        destpoint= new Point();
        destpoint.setLocation(mousex, mousey);
        origin = new Point();


        }


        for(int i = 0; i< Ptank.rapidfire; i++)
        {
            if (origin.distance(destpoint) <= 100 && origin.distance(destpoint) >= 50)
            {
                Bullet add = new Bullet(this,destpoint);
                add.getOrigin(origin);
                add.setPic(PlayerBullet);
                add.width=4;
                add.height=4;
                bulletList.add(add);
            }
        }

    }
}

public void mouseReleased(MouseEvent e)
{//Invoked when a mouse button has been released on a component.
}

public void mouseDragged(MouseEvent e)
{//Invoked when a mouse button is pressed on a component and then dragged.

}

public void mouseMoved(MouseEvent e)
{
    //Invoked when the mouse cursor has been moved onto a component but no buttons 
        Cursor cursor = Cursor.getDefaultCursor();
        //you have a List<Polygon>, so you can use this enhanced for loop
        cursor = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
        setCursor(cursor);
        mousex=e.getX();
        mousey=e.getY();

}

public void changebuttons(String x)
{
    if(x.equals("shop"))
    {
        menu.setBounds(720, 0, 80, 15);
        health.setBounds(0, 0, 125, 15);
        speed.setBounds(150, 0, 125, 15);
        shootradius.setBounds(300, 0, 200, 15);
        rapidfire.setBounds(500, 0, 150, 15);
        shop.setBounds(1000, 0, 150, 15);

    }


 }
Run Code Online (Sandbox Code Playgroud)

cam*_*ckr 5

KeyEvents 仅在具有焦点的组件上生成。当您单击按钮时,现在已获得焦点,不会在面板上生成按键事件。您需要添加:

panel.requestFocusInWindow()
Run Code Online (Sandbox Code Playgroud)

在您的 ActionListener 中将焦点返回给面板。

然而,更好的解决方案是使用键绑定,因为即使组件没有焦点,您也可以将绑定添加到 KeyStroke。