JButton 破坏了我的 Keylistener

发布于 2024-11-06 11:20:27 字数 4258 浏览 0 评论 0原文

JAVA 使用 NETBEANS

您好 stackoverflow,我有一个问题需要帮助。简而言之,我在 jpanel 上有一个鼠标监听器和一个按键监听器,一切正常,除了当我按下其中一个 jbutton 时,按键监听器擅离职守。任何人都可以解释这个问题吗?面板现在集中在按钮而不是键盘上,我很困惑。

这是代码,如果没有参考,假设它在那里,整个面板代码有 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);

    }


 }

JAVA USING NETBEANS

Hello stackoverflow, i have a problem i would like help with. In a nutshell, I have a mouselistener and a keylistener on a jpanel, everything works fine except when i press one of my jbuttons, then the keylistener goes AWOL. Can any1 explain the problem, is the panels focus now on the buttons instead of the keyboard, im at a lost.

Here is the code, if somethings are not reference, assume its are there the entire panel code was 500+ long so i cut quite a bit.

Thanks in advance for any help.

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

    }


 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾铮苏瑾 2024-11-13 11:20:27

KeyEvent 仅在具有焦点的组件上生成。当您单击按钮时,现在焦点将不会在面板上生成。您需要

panel.requestFocusInWindow()

在 ActionListener 中添加: 将焦点返回到面板。

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

KeyEvents are only generated on a component that has focus. When you click on the button is now has focus to key events won't be generated on the panel. You need to add:

panel.requestFocusInWindow()

in your ActionListener to give focus back to the panel.

However the better solution is to use Key Bindings as you can add bindings to a KeyStroke even when the component doesn't have focus.

简单爱 2024-11-13 11:20:27

不要使用需要组件集中工作的 KeyListener。相反,请考虑使用按键绑定。您可以在 Swing 教程中了解如何使用这些工具:如何使用键绑定。如果您需要更具体的帮助,您将需要发布比上面显示的小得多的代码,这些代码是独立的并且实际上会为我们编译和运行,一个 SSCCE.

Don't use a KeyListener which requires the component be focused to work. Instead consider using Key Bindings. You can find out how to use these guys at the Swing tutorial: How To Use Key Bindings. If you need more specific help, you will want to post a much smaller bit of code than you show above, code that is self-contained and will actually compile and run for us, an SSCCE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文