Java JButton弄坏了我的Keylistener
使用NETBEANS的JAVA 你好,我有一个问题需要帮助。简言之,我在jpanel上有一个鼠标侦听器和一个按键侦听器,一切正常,除了当我按下一个JButton时,按键侦听器就会擅离职守。任何人都能解释这个问题,是面板现在集中在按钮上而不是键盘上,我迷失了方向 这里是代码,如果有些东西不是参考,假设它在那里整个面板代码是500+长,所以我剪了很多 提前感谢您的帮助Java JButton弄坏了我的Keylistener,java,swing,listener,key-bindings,Java,Swing,Listener,Key Bindings,使用NETBEANS的JAVA 你好,我有一个问题需要帮助。简言之,我在jpanel上有一个鼠标侦听器和一个按键侦听器,一切正常,除了当我按下一个JButton时,按键侦听器就会擅离职守。任何人都能解释这个问题,是面板现在集中在按钮上而不是键盘上,我迷失了方向 这里是代码,如果有些东西不是参考,假设它在那里整个面板代码是500+长,所以我剪了很多 提前感谢您的帮助 package tankgame; public class TankPanel extends JPanel implement
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);
}
}
套装坦克游戏;
公共类TankPanel扩展JPanel实现KeyListener,
MouseListener,MouseMotionListener
{
JButton back、shop、menu、health、speed、rapidfire、shootradius;
专家小组()
{
setLayout(空);
addMouseListener(这个);
addMouseMotionListener(此);
addKeyListener(此);
设置聚焦(真);
车间=新的JButton(“车间”);
shop.addMouseListener(本);
车间立根(400,0,80,15);
添加(商店);
}
按下公共无效键(KeyEvent k)
{
char c=k.getKeyChar();
if(c==“u”)
{
u++;
System.out.println(u+“=u”);
}
如果(c=='i')
{
i++;
System.out.println(i+“=i”);
}
如果(c='d')
{
如果(Ptank.pic==PlayerTankE)
{
if(Ptank.move==true)
{
Pbarrel.x+=Ptank.speed;
Ptank.x+=Ptank.speed;
}
}
其他的
{
如果(Ptank.pic==PlayerTankN | | Ptank.pic==PlayerTankS)
{
Ptank.x=Ptank.x-5;
Ptank.y=Ptank.y+5;
}
Ptank.setPic(PlayerTankE);
p储罐宽度=35;
Ptank.height=23;
}
}
设置聚焦(真);
重新油漆();
}
公共无效密钥已释放(密钥事件k)
{
}
public void keyTyped(KeyEvent k)
{
}
公共无效mouseClicked(MouseEvent e)
{
//单击鼠标按钮时调用(按下并释放)
}
公共无效鼠标事件(鼠标事件e)
{//当鼠标进入组件时调用。
}
公共无效mouseExited(MouseEvent e)
{//当鼠标退出组件时调用。
}
公共无效鼠标按下(MouseEvent e)
{//在组件上按下鼠标按钮时调用。
如果(例如getSource()==返回)
{
System.out.println(456);
System.out.println(back.getLocation().x+“”+back.getLocation().y);
}
else if(如getSource()==菜单)
{
更改按钮(“菜单”);
System.out.println(456);
System.out.println(menu.getLocation().x+“”+menu.getLocation().y);
}
else if(e.getSource()==shop)
{
更换按钮(“商店”);
System.out.println(456);
System.out.println(shop.getLocation().x+“”+shop.getLocation().y);
}
else if(e.getButton()==MouseEvent.BUTTON1)
{
destpoint=新点();
destpoint.setLocation(mousex,mousey);
原点=新点();
}
对于(int i=0;i
不要使用要求组件集中工作的KeyListener。取而代之的是考虑使用键绑定。你可以在Swing教程中找到如何使用这些家伙:。如果您需要更具体的帮助,您将希望发布比上面显示的代码小得多的代码,这些代码是自包含的,并且实际上将为我们编译和运行。不要使用KeyListener,因为它要求组件集中精力才能工作。取而代之的是考虑使用键绑定。你可以在Swing教程中找到如何使用这些家伙:。如果您需要更具体的帮助,您将希望发布比上面显示的代码小得多的代码,这些代码是自包含的,并且将实际为我们编译和运行。KeyEvents仅在具有焦点的组件上生成。当你点击按钮是现在有焦点的关键事件将不会在面板上生成。您需要添加:
panel.requestFocusInWindow()
在ActionListener中,将焦点返回到面板
但是,更好的解决方案是使用,因为即使在组件没有焦点的情况下,也可以向击键添加绑定。KeyEvents仅在有焦点的组件上生成。当你点击按钮是现在有焦点的关键事件将不会在面板上生成。您需要添加:
panel.requestFocusInWindow()
在ActionListener中,将焦点返回到面板
但是,更好的解决方案是使用,因为即使组件没有焦点,您也可以为击键添加绑定。Rob,因为他只听几个不同的键,不是吗