【问题标题】:The type [classname] must implement the inherited abstract method KeyListener.keyTyped(KeyEvent)类型 [classname] 必须实现继承的抽象方法 KeyListener.keyTyped(KeyEvent)
【发布时间】:2012-08-21 11:44:15
【问题描述】:

java:我卡在错误上 Pong.Move1 类型必须实现继承的抽象方法 KeyListener.keyTyped(KeyEvent) 当我使用 keyListener 时。没看懂是什么意思?帮忙?

这是我遇到问题的主题...

private class Move1 extends Thread implements KeyListener{
  public void run(){
    addKeyListener(this);
    while(true){
      //hitRight makes you lose.
      //point is how many times it ricochets.
      if(ball.intersects(borderRight)){
        hitRight = true;
      }
      if(ball.intersects(borderLeft)){
        point++;
      }
  }

}
public void keyPressed(KeyEvent event){
while(event.getKeyCode()==40||event.getKeyCode()=='s'){
direction = DOWN;
Thread.sleep(500);
}
}
public void KeyReleased(KeyEvent event){

}
public void KeyTyped(KeyEvent event){

}

}

我也被困在Thread.sleep(500); 线上。它说未处理的异常类型InterruptedException。有什么帮助吗?谢谢。

哦,我忘了一件事。
1:当我尝试运行它时,我得到的唯一错误是:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: at Pong.main(Pong.java:50)。那是public static void main(String[] args) 行。我的完整代码在最后,所以你可以看看它(plz)
2:我正在使用eclipse。
3:我基本上是一个初学者(不是真的)

我的完整代码:

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.EventListenerList;
public class Pong extends JFrame{
    public final int WIDTH = 1000, HEIGHT = 1000;
    final int UP = 1, DOWN = 2;
    boolean hitRight;
    int point = 0;
    int direction;
    Rectangle bg = new Rectangle(0,0,WIDTH,HEIGHT);
    Rectangle borderLeft = new Rectangle(0,0,WIDTH/320,HEIGHT);
    Rectangle borderRight = new Rectangle(WIDTH-WIDTH/320,0,WIDTH/320,HEIGHT);
    Rectangle borderTop = new Rectangle(borderLeft.x,borderLeft.y,WIDTH,HEIGHT/35);
    Rectangle borderBottom = new Rectangle(0,HEIGHT-HEIGHT/320,WIDTH,HEIGHT/320);
    Rectangle ball = new Rectangle(WIDTH/2,HEIGHT/2,WIDTH/64,HEIGHT/64);
    Rectangle board = new Rectangle(WIDTH-WIDTH/160,0,WIDTH/128,HEIGHT/10);
public void paint(Graphics graphics){
super.paint(graphics);
graphics.setColor(Color.BLACK);
graphics.fillRect(bg.x,bg.y,bg.width,bg.height);
graphics.setColor(Color.RED);
graphics.fillRect(borderLeft.x, borderLeft.y, borderLeft.width, borderLeft.height);
graphics.fillRect(borderRight.x, borderRight.y, borderRight.width, borderRight.height);
graphics.fillRect(borderTop.x, borderTop.y, borderTop.width, borderTop.height);
graphics.fillRect(borderBottom.x, borderBottom.y, borderBottom.width, borderBottom.height);
graphics.setColor(Color.WHITE);
graphics.fillRect(ball.x,ball.y,ball.width,ball.height);
graphics.fillRect(board.x,board.y,board.width,board.height);
}
    /**
     * This Pong game made by me.
     * This has no copied code.
     * Any similarities are coincidences.
     * @param args
     */
    /*
     * The constructor.
     */
    public Pong(){
    super("Pong");
    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Pong p = new Pong();
    }
    /*
     * The move thread.
     */
private class Move1 extends Thread implements KeyListener{
    public void run(){
        addKeyListener(this);
            while(true){
        //hitRight makes you lose.
        //point is how many times it ricochets.
    if(ball.intersects(borderRight)){
        hitRight = true;
    }
    if(ball.intersects(borderLeft)){
        point++;
    }
                    }

        }
    public void keyPressed(KeyEvent event){
    while(event.getKeyCode()==40||event.getKeyCode()=='s'){
    direction = DOWN;
    Thread.sleep(500);
    }
    }
    public void KeyReleased(KeyEvent event){

    }
    public void KeyTyped(KeyEvent event){

    }

    }
}
/*
 * End of move thread...
 */

【问题讨论】:

  • 问题是什么?第一个还是第二个?
  • 为什么在编译过程中出现错误时希望代码运行?
  • 我没有说我期望它会运行。我说当我尝试运行它时出现错误,我还询问了错误是什么。就是这样。
  • 但是,编译错误是什么?

标签: java keylistener


【解决方案1】:

您的第一个错误,与 KeyListener 相关,只是一个案例错误。在Java中,方法和变量以camelCase命名,并且语言区分大小写,所以你必须将你的方法定义为keyTyped而不是KeyTyped。这同样适用于keyReleased

至于您的第二个错误,您必须进行必要的更改以处理声明为由 Thread#sleep 抛出的InterruptedException。您可以将该方法调用包含在 try-catch 块中。 (但是,请注意,KeyEvent 的键码永远不会改变,所以你有一个潜在的无限 while 循环。)

我建议阅读 a tutorial 或两篇关于实现关键监听器的文章。

【讨论】:

  • 你会知道编译错误的原因吗?顺便谢谢。
  • 编译错误是因为运行了一个没有成功编译的程序。
  • 好的,我想是括号的问题,我修好了,再次感谢。
【解决方案2】:

为了实现 KeyListener,您需要实现该单个接口包含的所有方法。 它们是:

keyPressed
keyDown
keyTyped

为了使用接口,您必须实现它们的所有方法。

此外,Thread.sleep(500) 可能会产生异常。 Java 基本上要求您处理错误,以防万一出现问题。为此,您需要一个 try...catch,如下所示:

try
{
    Thread.sleep(500);
}
catch(InterruptedException e)
{
    System.out.println("Error!");
}

【讨论】:

  • 好的,我知道了,是括号...失败。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多