我的keyListener发生了什么?

时间:2017-12-17 03:53:07

标签: java

在下面的代码中,我试图创建一个带有"白点"代表青蛙。然而,一切顺利,直到我添加keyListener,我刚刚从观看了severl youtube视频。问题是" FINAL"在公共课中有红色下划线。我按照我观看的视频做了一切;我有" keyPress" ...我确实相信使用"这是"在public void init中。有人请帮忙。

import java.applet.Applet;
import java.awt.BasicStroke;
import java.awt.Graphics;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class FINAL extends Applet implements Runnable,KeyListener{
Thread t;
Image BGImage;
Image i;
Graphics DG;
int x =0;
int y = 100;
int a;
int b;
int c;
int d;
int e;
int f;

int h = 20;
int j = 480;

public void init()
{
    this.addKeyListener(this);  
    setSize(500,500);
  BGImage = getImage(getCodeBase(),"frogger_background.jpg");

}

    public void start()
     {
        if(t==null)
        {
         t=new Thread(this,"New Thread");
         t.start();
        }
     }

    public void stop()
    {
        if(t!=null)
        {
         t=null;
        }
    }

    public void run()
    {
        Thread t1=Thread.currentThread();
        while(t==t1)  
        { 
         repaint();
         try
         {  
          Thread.sleep(100); //slepp 100 ms
         }
         catch(Exception e)
         {   }
        }
    }

    @Override
 public void keyPressed(KeyEvent e){
     //int keyCode = e.getKeyCode();
     switch (e.getKeyCode()) {
        case KeyEvent.VK_DOWN: 
            j = j + 1;
            break;

        case KeyEvent.VK_UP: 
            j = j - 1;
            break;

        case KeyEvent.VK_LEFT: 
            h = h - 1;
            break;

        case KeyEvent.VK_RIGHT:
            h = h + 1;
            break;
    }
 }

@Override
 public void keyReleased(KeyEvent e){

 }

 @Override
public void update(Graphics g){
        i = createImage(this.getSize().width,this.getSize().height);
        DG = i.getGraphics();
    paint(DG);
        g.drawImage(i,0,0,500,500,this);
}



@Override
 public void paint(Graphics g){
     //Graphics2D g2 = (Graphics2D)g;
     x = x+1;
     a = x%500+30;
     b = 5*(x%500);
     c = 3*(x%500);
     d = 4*(x%500)+50;
     e = 6*(x%500)+10;
     f = 7*(x%500)+20;
     g.drawImage(BGImage, 0, 0, this);

     g.setColor(new Color(160,82,45));
     g.fillRoundRect(a, y, 120, 35, 5, 5);

          g.setColor(new Color(160,82,45)); //Wood#2
     g.fillRoundRect(b, y+50, 120, 35, 5, 5);


          g.setColor(new Color(160,82,45)); //Wood#3
     g.fillRoundRect(c, y+100, 120, 35, 5, 5);


          g.setColor(Color.RED);
     g.fillRoundRect(d, 300, 60, 25, 5, 5);


          g.setColor(Color.BLUE);
     g.fillRoundRect(e, 350, 60, 25, 5, 5);


          g.setColor(Color.YELLOW);
     g.fillRoundRect(f, 400, 60, 25, 5, 5);

          g.setColor(Color.WHITE);
     g.fillOval(j,h,20,20);
     if (j<0){

     }
     System.out.println("j =" + j);
     System.out.println("h =" + h);

}

}

1 个答案:

答案 0 :(得分:2)

FINAL不符合KeyListener interface的合同要求,特别是keyTyped

您需要添加...

@Override
public void keyTyped(KeyEvent e) {
}

到你的班级

Applet也已弃用,您应该避免使用它。

我建议您在SwingJavaFX投入时间