java keylistener有什么问题?

时间:2013-10-10 14:12:29

标签: java swing applet keyevent mixing

我不断收到错误消息:

Multiple markers at this line
    - The type Main must implement the inherited abstract method     KeyListener.keyTyped(KeyEvent)
    - The serializable class Main does not declare a static final serialVersionUID field of type     long
    - The type Main must implement the inherited abstract method     KeyListener.keyPressed(KeyEvent)

我该怎么办?

这是我的代码:(我在错误位置旁注释)

`package drawLines;

import java.util.Date;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;


**/*here appears the error message*/**public class Main extends Applet implements KeyListener {

    boolean right=true;
    boolean left=false;
    boolean up=false;
    boolean down=false;
    boolean inGame=true;

    public void listen(){
        addKeyListener((KeyListener) this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    public void actionPerformed(KeyEvent e){
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            left=true;
            up=false;
            down=false;
        }

        if (key == KeyEvent.VK_RIGHT) {
            right=true;
            up=false;
            down=false;
        }

        if (key == KeyEvent.VK_UP) {
            up=true;
            right=false;
            left=false;
        }

        if (key == KeyEvent.VK_DOWN) {
            down=true;
            right=false;
            left=false;
        }


    }

    public void keytyped(KeyEvent e){}
    public void keyReleased(KeyEvent e){}




    int x1=5;
    int y1=5;
    int x2=x1+5;
    int y2=y1+5;


    public int moveRight(){
        return ++x1;
    }

    public int moveLeft(){
        return --x1;
    }

    public int moveUp(){
        return ++y1;
    }

    public int moveDown(){
        return --y1;
    }

    public void paint1(Graphics g){
        g.drawOval(x1,y1,x2,y2);
    }



    public void paint(Graphics e){

        while (right=true){
        long millis =System.currentTimeMillis();
        long millisn =System.currentTimeMillis();

            while (millisn<millis+20){
                millisn=System.currentTimeMillis();
            }

        e.setColor(Color.white);
        e.drawOval(x1,y1,x2,y2);
        e.setColor(Color.red);
        moveRight();
        e.drawOval(x1,y1,x2,y2);

        }
        while(inGame==true){
            if(right==true){
                long millis =System.currentTimeMillis();
                long millisn =System.currentTimeMillis();

                    while (millisn<millis+20){
                        millisn=System.currentTimeMillis();
                    }

                e.setColor(Color.white);
                e.drawOval(x1,y1,x2,y2);
                e.setColor(Color.red);
                moveRight();
                e.drawOval(x1,y1,x2,y2);
            }

            else if(down==true){
                    long millis =System.currentTimeMillis();
                    long millisn =System.currentTimeMillis();

                        while (millisn<millis+20){
                                    millisn=System.currentTimeMillis();
                        }

                    e.setColor(Color.white);
                    e.drawOval(x1,y1,x2,y2);
                    e.setColor(Color.red);
                    moveDown();
                    e.drawOval(x1,y1,x2,y2);

            }
                else if (left==true){
                    long millis =System.currentTimeMillis();
                    long millisn =System.currentTimeMillis();

                        while (millisn<millis+20){
                            millisn=System.currentTimeMillis();
                        }

                    e.setColor(Color.white);
                    e.drawOval(x1,y1,x2,y2);
                    e.setColor(Color.red);
                    moveLeft();
                    e.drawOval(x1,y1,x2,y2);


                    }}
    }

    public static void main(String[] args) {

    }    


        }

`

2 个答案:

答案 0 :(得分:2)

错误说明了一切:

Main类型必须实现继承的抽象方法KeyListener.keyTyped(KeyEvent)

  • 你需要实现抽象方法,因为这个只是纠正拼写,就像JavaBanana所说的那样。

可序列化类Main不声明long

类型的静态最终serialVersionUID字段
  • 这是您必须在课程中添加的ID,只需创建一个或google示例ID

Main类型必须实现继承的抽象方法KeyListener.keyPressed(KeyEvent)

  • 您需要再次实现抽象方法keyPressed。我的意思是你必须定义并实现一种在这种情况下将被调用的方法。

许多IDE都会处理这个问题,甚至会为你创建方法声明,给他们一个机会。 (我使用netbeans)

答案 1 :(得分:1)

你拼写方法“keytyped(KeyEvent e)”但它必须是“keyTyped(KeyEvent e)

“T”必须为大写:)

用keyPressed()方法替换你的actionPerformed方法..