使用KeyListener时出现AWT-EventQueue-0错误

时间:2015-01-15 14:19:56

标签: java events awt

我制作了2个与keylisteners一起移动的对象。它们正确移动,但在控制台中,每次按下按钮时都会收到此错误:

  

线程“AWT-EventQueue-0”中的异常   java.lang.UnsupportedOperationException:尚不支持。在   CatchMe.CatchMe.keyTyped(CatchMe.java:197)

我的代码如下:

package CatchMe;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.Timer;

public class CatchMe extends JFrame implements ActionListener, KeyListener {

private Image dbImage;
private Graphics dbg;
int x = 200, y = 300;
int velX, velY;
int velX1, velY1;
int ScoreB1 = 0;
int ScoreR1 = 0;
Timer tm = new Timer(5, this);
long startTime = System.currentTimeMillis();
long onGoing;
String onG;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
int recX = (width + 100) / 2, recY = (height + 100) / 2;

public CatchMe() {
    setSize(width, height);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
    tm.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
}

@Override
public void paint(Graphics g) {
    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);
}

public void paintComponent(Graphics g) {
    Rectangle r1 = new Rectangle(recX, recY, 25, 25);
    Rectangle r2 = new Rectangle(x, y, 25, 25);
    g.setColor(Color.BLUE);
    g.fillRect(r1.x, r1.y, r1.width, r1.height);
    g.setColor(Color.RED);
    g.fillRect(r2.x, r2.y, r2.width, r2.height);
    g.setColor(Color.BLACK);
    g.fillRect(0, 20, 100, 50);
    g.fillRect(100, 20, 100, 50);
    g.fillRect(200, 20, 50, 50);
    g.setColor(Color.YELLOW);
    g.drawString("Score of Blue", 5, 40);
    String ScoreB = ScoreB1 + "";
    g.drawString(ScoreB, 5, 55);
    g.drawString("Score of Red", 110, 40);
    String ScoreR = ScoreR1 + "";
    g.drawString(ScoreR, 110, 55);
    g.drawLine(95, 20, 95, 69);
    g.drawLine(200, 20, 200, 69);
    g.setColor(Color.RED);
    onGoing = (System.currentTimeMillis() - startTime) / 1000;
    onG = onGoing + "";
    g.setColor(Color.YELLOW);
    g.drawString(onG, 220, 50);
    System.out.println(onGoing);
    if (r1.intersects(r2)) {
        if (onGoing <= 60 || (onGoing >= 120 && onGoing < 180)) {
            g.setColor(Color.BLUE);
            g.drawString("Blue Caught You!", 175, 90);
            ScoreB1++;

        } else if (onGoing >= 240) {
            if (ScoreB1 > ScoreR1) {
                Font font = new Font("Jokerman", Font.PLAIN, 50);
                g.setColor(Color.BLUE);
                g.drawString("BLUE WINS", 600, 400);
            } else if (ScoreB1 < ScoreR1) {
                Font font = new Font("Jokerman", Font.PLAIN, 50);
                g.setColor(Color.RED);
                g.drawString("RED WINS", 600, 400);
            }
        } else {
            g.setColor(Color.RED);
            g.drawString("Red Caught You!", 175, 90);
            ScoreR1++;
        }
        if (onGoing == 245) {

            System.exit(245);
        }
        repaint();
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    x = x + velX;
    y = y + velY;
    recY += velY1;
    recX += velX1;
    if (x < 0) {
        velX = 0;
        x = 0;
    }
    if (x > width - 50) {
        velX = 0;
        x = width - 50;
    }
    if (y < 0) {
        velY = 0;
        y = 0;
    }
    if (y > height - 40) {
        velY = 0;
        y = height - 40;
    }
    //Second square
    if (recX < 0) {
        velX1 = 0;
        recX = 0;
    }
    if (recX > width - 50) {
        velX1 = 0;
        recX = width - 50;
    }
    if (recY < 0) {
        velY1 = 0;
        recY = 0;
    }
    if (recY > height - 40) {
        velY1 = 0;
        recY = height - 40;
    }
    repaint();
}

@Override
public void keyPressed(KeyEvent e) {
    int code = e.getKeyCode();
    if (code == KeyEvent.VK_LEFT) {
        velX = -5;
        velY = 0;
    }
    if (code == KeyEvent.VK_UP) {
        velX = 0;
        velY = -5;
    }
    if (code == KeyEvent.VK_RIGHT) {
        velX = 5;
        velY = 0;
    }
    if (code == KeyEvent.VK_DOWN) {
        velX = 0;
        velY = 5;
    }
    //Second Rect
    if (code == KeyEvent.VK_A) {
        velX1 = -5;
        velY1 = 0;
    }
    if (code == KeyEvent.VK_W) {
        velX1 = 0;
        velY1 = -5;
    }
    if (code == KeyEvent.VK_D) {
        velX1 = 5;
        velY1 = 0;
    }
    if (code == KeyEvent.VK_S) {
        velX1 = 0;
        velY1 = 5;
    }
}

@Override
public void keyReleased(KeyEvent e) {
    velX = 0;
    velY = 0;
    velX1 = 0;
    velY1 = 0;
}

public static void main(String[] args) {
    CatchMe main = new CatchMe();
}

@Override
public void keyTyped(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}

2 个答案:

答案 0 :(得分:1)

@Override
public void keyTyped(KeyEvent e) {

}

修复它,你在调用keyTyped时手动抛出异常。

答案 1 :(得分:1)

keyTyped方法抛出UnsupportedOperationException因为您以这种方式实现它(或者它是以这种方式自动生成的):

@Override
public void keyTyped(KeyEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

每当键入一个键时,都会调用此方法,因此会抛出异常。

要修复它,您需要更改此方法的主体。如果你不想让它做任何事情,只需将它的身体留空:

@Override
public void keyTyped(KeyEvent e) {

}

如果您想要做某事,您应该根据所需的行为实现此方法。例如,每次键入密钥时,此实现都会打印一条消息:

 @Override
 public void keyTyped(KeyEvent e) {
     System.out.println("A key was typed");
 }