Вопрос

I keep getting an error message:

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)

what should I do?

this is my code:(I commented beside the error place)

`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) {

    }    


        }

`

Это было полезно?

Решение

The errors say it all:

The type Main must implement the inherited abstract method KeyListener.keyTyped(KeyEvent)

  • You need to implement the abstract methods, for this one just correct the spelling like JavaBanana said.

The serializable class Main does not declare a static final serialVersionUID field of type long

  • This is an id you must include in the class, just create one or google an example ID

The type Main must implement the inherited abstract method KeyListener.keyPressed(KeyEvent)

  • Again you need to implement the abstract method keyPressed. By that I mean you must define and implement a method that will be called in that situation.

Many IDE's take care of this and will even create the method declaration for you, give them a shot. (I use netbeans)

Другие советы

You spelled the method "keytyped(KeyEvent e)" but it has to be "keyTyped(KeyEvent e)

The "T" must be uppercase :)

And replace your actionPerformed method with the keyPressed() method..

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top