简体   繁体   English

KeyListener的AWT-EventQueue-0错误

[英]AWT-EventQueue-0 Error with KeyListener

I made 2 objects that moves with keylisteners. 我制作了2个可与键侦听器一起移动的对象。 They move correctly, but in the console I receive this error every time I press a button: 它们可以正确移动,但是每次按下按钮时,在控制台中我都会收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet. 线程“ AWT-EventQueue-0”中的异常java.lang.UnsupportedOperationException:尚不支持。 at CatchMe.CatchMe.keyTyped(CatchMe.java:197) 在CatchMe.CatchMe.keyTyped(CatchMe.java:197)

My code is as follows: 我的代码如下:

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.
}
}
@Override
public void keyTyped(KeyEvent e) {

}

Will fix it, you were manually throwing exception when keyTyped is called. 将解决它,您在调用keyTyped时手动引发了异常。

The keyTyped method throws an UnsupportedOperationException because you implemented it this way(or it was automatically generated this way): keyTyped方法引发UnsupportedOperationException因为您是通过这种方式实现的(或者它是通过这种方式自动生成的):

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

Whenever a key is typed, this method is called, so it throws an exception. 每当键入键时,都会调用此方法,因此会引发异常。

To fix it, you need to change the body of this method. 要修复它,您需要更改此方法的主体。 If you don't want it to do anything, just leave its body empty: 如果您不希望它做任何事情,请将其主体留空:

@Override
public void keyTyped(KeyEvent e) {

}

If you want to do something, you should implement this method according to the desired behavior. 如果要执行某些操作,则应根据所需的行为实现此方法。 For example, this implementation prints a message every time a key is typed: 例如,此实现每次键入键时都会打印一条消息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM