我正在尝试完成我的高中开始编程课程的代码,我很困惑Java告诉我它找不到Listener
(当我已经导入它的时候?)。再次尝试使用import java.awt.event.*;
后,它仍无法正常工作。有人可以帮我解决这个加剧的问题吗?
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import javax.swing.Timer;
public class PrizePanelA extends JPanel {
private static final int FRAME = 400;
private static final Color BACKGROUND = new Color(204, 204, 204);
private BufferedImage myImage;
private Graphics myBuffer;
private Ball ball;
private Polkadot prize;
private int hits;
private Timer c;
private Bumper bumper1;
private Bumper bumper2;
public PrizePanelA() {
myImage = new BufferedImage(400, 400, 1);
myBuffer = this.myImage.getGraphics();
ball = new Ball(200, 200, 50, Color.black);
ball.jump(400, 400);
bumper1 = new Bumper(50, 170, 50, 50, Color.yellow);
prize = new Polkadot(200, 200, 25, Color.red);
prize.jump(400, 400);
hits = 0;
addKeyListener(new Key());
setFocusable(true);
c = new Timer(10, new Listener());
c.start();
}
private class Key extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if ((e.getKeyCode() == 38)) {
prize.setY(prize.getY() + 10);
}
}
public void paintComponent(Graphics g) {
g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);
}
private class Listener implements ActionListener {
private Listener() {}
public void actionPerformed(ActionEvent e) {
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0, 0, 400, 400);
ball.move(400.0, 400.0);
collide(ball, prize);
ball.draw(myBuffer);
prize.draw(myBuffer);
myBuffer.setColor(Color.BLACK);
myBuffer.setFont(new Font("Monospaced", 1, 24));
myBuffer.drawString("Count: " + hits, 250, 25);
repaint();
}
}
public void collide(Ball b, Polkadot pd) {
double d = distance(b.getX(), b.getY(), pd.getX(), pd.getY());
if (d <= b.getDiameter() / 2 + pd.getDiameter() / 2) {
pd.jump(400, 400);
hits += 1;
}
}
private double distance(double x1, double y1, double x2, double y2) {
return Math.sqrt(Math.pow(x1 - x2, 2.0) + Math.pow(y1 - y2, 2.0));
}
}
}
错误:
PrizePanelA.java:37: error: cannot find symbol
c = new Timer(10, new Listener());
^
symbol: class Listener
location: class PrizePanelA
答案 0 :(得分:2)
您的Listener
类位于Key
类中,因此将其移到Key
类的范围之外,因为如果没有外部类的实例化,则无法使用非静态内部类。
// move it outside of class Key
private class Listener implements ActionListener {
private Listener() {}
public void actionPerformed(ActionEvent e) {
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0, 0, 400, 400);
ball.move(400.0, 400.0);
collide(ball, prize);
ball.draw(myBuffer);
prize.draw(myBuffer);
myBuffer.setColor(Color.BLACK);
myBuffer.setFont(new Font("Monospaced", 1, 24));
myBuffer.drawString("Count: " + hits, 250, 25);
repaint();
}
}
private class Key extends KeyAdapter {
参考:
What is the difference between static and non static inner class?
JIC:如果你想使用当前的实现,那么就像我说的那样,使用外部类实例c = new Timer(10, new Key().new Listener());