KeyListener出现问题

时间:2015-05-21 16:42:49

标签: java swing awt keylistener

这是我第一次使用KeyListener,如果我犯了一个明显的错误,请原谅我。我的问题是每当我按下箭头键或WASD键时,省略号都没有反应。我知道我的代码中还有其他缺陷,但我只想关注KeyListener问题。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.KeyListener;

public class idkdude extends JPanel implements ActionListener, KeyListener {

    Timer t = new Timer(5, this);
     int rx = 395, ry = 395, rvelx = 0, rvely = -2, bx = 195, by = 195, bvelx =     0, bvely = 2;
     boolean redWins = false;
     boolean blueWins = false;

     public void paint(Graphics g)
        {
            super.paint(g);
             drawSurface(g);
            Graphics2D g2 = (Graphics2D) g;             
            g.setColor(Color.red);
            Ellipse2D rcircle = new Ellipse2D.Double(rx, ry, 10, 10);
            g2.fill(rcircle);
            g2.drawLine(400, 400, rx+5, ry+5);
            g.setColor(Color.blue);
            Ellipse2D bcircle = new Ellipse2D.Double(bx, by, 10, 10);
            g2.fill(bcircle);
            g2.drawLine(200, 200, bx+5, by+5);
            if (redWins == true)
            {
                redWins(g);
            }
            if (blueWins == true)
            {
                blueWins(g);
            }
            t.start();
        }

     public void init()
     {
         addKeyListener(this);
     }
     public void keyReleased(KeyEvent e){}
        public void keyTyped(KeyEvent e) {}
        public void keyPressed(KeyEvent e)
        { //make sure to put
            //rx+=rvelx;
            //ry+=rvely;
            // and draw a new line
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_LEFT) 
            {rvelx = -2;rvely = 0;}
            if (key == KeyEvent.VK_RIGHT)
            {rvelx = 2; rvely = 0;}
            if (key == KeyEvent.VK_UP)
            {rvelx = 0; rvely = -2;}
            if (key == KeyEvent.VK_DOWN)
            {rvelx = 0; rvely = 2;}
            if (key == KeyEvent.VK_W)
            {bvelx = 0; rvely = -2;}
            if (key == KeyEvent.VK_A)
            {bvelx = -2; rvely = 0;}
            if (key == KeyEvent.VK_S)
            {bvelx = 0; rvely = 2;}
            if (key == KeyEvent.VK_D)
            {bvelx = 2; rvely = 0;}
        }
     public void actionPerformed(ActionEvent e)
        {
            rx+=rvelx;
            ry+=rvely;
            bx+=bvelx;
            by+=bvely;
            if (rx> 600 || rx <0)
            {
                blueWins = true;
            }

            if (ry<0 || ry>600)
            {
                blueWins = true;
            }
            if (bx>600 || bx<0)
            {
                redWins = true;
            }
            if (by>600 || by<0)
            {
                redWins = true;             
            }                
            repaint();              
        }

     public void redWins(Graphics g)
     {
         g.setColor(Color.black);
            g.fillRect(0,0,600,600);
            g.setColor(Color.white);
            g.drawString("RED HAS WON!!!!!", 250, 250);
            g.drawString("Click Anywhere to Start Another Game", 250, 350);  
     }
     public void blueWins(Graphics g)
     {
         g.setColor(Color.black);
            g.fillRect(0,0,600,600);
            g.setColor(Color.white);
            g.drawString("BLUE HAS WON!!!!!", 250, 250);
            g.drawString("Click Anywhere to Start Another Game", 250, 350);
     }
     public void drawSurface(Graphics g)
     {
        g.setColor(Color.black);
        g.fillRect(0,0,600,600);
        g.setColor(Color.white);
        for (int x = 0; x <= 600; x+=25)
            g.drawLine(x,0,x,600);
        for (int y= 0; y <=600; y+=25)
            g.drawLine(0,y,600,y);
     }
}

1 个答案:

答案 0 :(得分:0)

KeyListener

上的键绑定

使用Key bindings的恕我直言是一种更灵活,更可靠的方法,带来了这些好处:

话虽如此,您可以创建KeyStrokes,如下所示:

KeyStroke upKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
KeyStroke leftKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
KeyStroke downKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
KeyStroke rightKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);

您还可以指定在键释放事件上触发击键:

KeyStroke upReleasedKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true);

注意: 0表示没有修饰符,true是一个标志,表示键击表示键释放事件。有关详细信息,请参阅the API

最后,您应该使用WHEN_IN_FOCUSED_WINDOW条件将不同的操作附加到面板的输入映射(面板不可聚焦)。

另请参阅此主题:Key bindings vs. key listeners in JavaHow to Use Key Bindings教程

进行自定义绘画

请注意,您不应该覆盖paint(...)方法,而应覆盖paintComponent(...)。见A Closer Look at the Paint Mechanism