簡體   English   中英

java keylistener有什么問題?

[英]what is wrong with the java keylistener?

我不斷收到錯誤消息:

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

    }    


        }

`

錯誤說明了一切:

Main類型必須實現繼承的抽象方法KeyListener.keyTyped(KeyEvent)

  • 你需要實現抽象方法,因為這只是像JavaBanana所說的那樣糾正拼寫。

可序列化類Main不聲明long類型的靜態最終serialVersionUID字段

  • 這是您必須在課程中添加的ID,只需創建一個或google示例ID即可

Main類型必須實現繼承的抽象方法KeyListener.keyPressed(KeyEvent)

  • 您需要再次實現抽象方法keyPressed。 我的意思是你必須定義並實現一個在這種情況下將被調用的方法。

許多IDE都會處理這個問題,甚至會為你創建方法聲明,給他們一個機會。 (我使用netbeans)

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

“T”必須是大寫的:)

並使用keyPressed()方法替換您的actionPerformed方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM