2013-10-10 79 views
1

我不斷收到一條錯誤消息: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) { 

    }  


     } 

`

+1

實現它們! – RamonBoza

回答

2

的錯誤說明了一切:

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

  • 您需要實現的抽象方法,因爲這只是一個喜歡糾正拼寫JavaBanana說。

的序列化類主要不聲明類型長

  • 這是一個id,你必須包含在類,只需創建一個或谷歌的例子ID的靜態最後的serialVersionUID領域

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

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

許多IDE都會照顧到這一點,甚至會爲您創建方法聲明,給他們一個鏡頭。 (我用netbeans)

+0

非常感謝你!你幫了很多忙! – Atlantis

1

你拼寫法 「的keyTyped(KeyEvent的E)」,但它必須是「的keyTyped(KeyEvent e而)

的 「T」 必須大寫:)

而且隨着的keyPressed()方法替換您的actionPerformed方法..