gpt4 book ai didi

java - 类声明范围不明确

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:09:38 25 4
gpt4 key购买 nike

我正在尝试完成我的高中初级编程类(class)的代码,我很困惑 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

最佳答案

你的 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());

关于java - 类声明范围不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47604758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com