野生的猴 2021-06-03 20:35 采纳率: 100%
浏览 158
已采纳

KeyEvent cannot be resolved to a type是什么原因啊

package sb1;

import java.awt.Graphics;

import java.awt.event.KeyListener;

import javax.swing.JPanel;

import java.awt.*;

import javax.swing.*;

public class SB0 extends JPanel implements KeyListener{

 int lenght;

 int[] she_x=new int[500];

 int[] she_y=new int[500];

 String fx;

 boolean start=false;

public SB0() {

 init();

}

public void init() {

  lenght=3;

  fx="r";

  she_x[0]=100;she_y[0]=100;

  she_x[1]=75;she_y[1]=100;

  she_x[2]=50;she_y[2]=100;

  she_x[3]=25;she_y[3]=100;

}

 protected void paintComponent(Graphics g) {

 super.paintComponent(g);

 int a=1;

 new ImageIcon("jpg/狗头_900_100.jpg").paintIcon(this,g,a,1);

 g.fillRect(25,75, 850,600);

 if(fx.equals("r")) {

  new ImageIcon("jpg/right.png").paintIcon(this,g ,she_x[0]+25 ,she_y[0] );

 }else if(fx.equals("1")) {

  new ImageIcon("jpg/left.png").paintIcon(this,g ,she_x[0]+25 ,she_y[0] );

 }else if(fx.equals("u")) {

  new ImageIcon("jpg/up.png").paintIcon(this,g ,she_x[0]+25 ,she_y[0] );

 

 }else if(fx.equals("d")) {

  new ImageIcon("jpg/down.png").paintIcon(this,g ,she_x[0]+25 ,she_y[0] );

 

 }

 for(int i=1;i<=lenght;i++) {

  new ImageIcon("jpg/boyd.png").paintIcon(this,g ,she_x[i] ,she_y[i] );

 }

 if(start==false) {

  g.setColor(Color.white);

  g.setFont(new Font("微软雅黑",Font.BOLD,40));

  g.drawString("按下夏福特开始游戏", 300, 300);

 }

 }

 public void keyPressed(KeyEvent e) {

    

  }

}

  • 写回答

3条回答 默认 最新

  • CSDN专家-Fay 2021-06-03 20:40
    关注

    KeyListener 接口里面没有keyPressed方法,只有keyTyped和keyReleased,你是要监听按键还是按钮

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?