【发布时间】:2013-11-15 15:06:10
【问题描述】:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
class Ball {
int x, y, radius, dx, dy;
Color BallColor;
public Ball(int x, int y, int radius, int dx, int dy, Color bColor) {
this.x = x;
this.y = y;
this.radius = radius;
this.dx = dx;
this.dy = dy;
BallColor = bColor;
}
}
public class Bounce extends Applet implements Runnable {
Ball redBall;
public void init() {
redBall = new Ball(250, 80, 50, 2, 4, Color.red);
Thread t = new Thread(this);
t.start();
}
public void paint(Graphics g) {
g.setColor(redBall.BallColor);
setBackground(Color.pink);
g.fillOval(redBall.x, redBall.y, redBall.radius, redBall.radius);
g.drawLine(150, 400, 50, 500);
g.drawLine(150, 400, 450, 400);
g.drawLine(50, 500, 350, 500);
g.drawLine(450, 400, 350, 500);
g.drawRect(50, 500, 20, 100);
g.drawRect(330, 500, 20, 100);
g.drawLine(450, 400, 450, 500);
g.drawLine(430, 500, 450, 500);
g.drawLine(430, 500, 430, 420);
}
public void run() {
while (true) {
try {
displacementOperation(redBall);
Thread.sleep(20);
repaint();
} catch (Exception e) {
}
}
}
public void displacementOperation(Ball ball) {
if (ball.y >= 400 || ball.y <= 0) {
ball.dy = -ball.dy;
}
ball.y = ball.y + ball.dy;
}
}
当我编译并运行代码时,它说在类 Bounce 中找不到 main 方法,请将方法定义为 public static void main(String[] args)。如果有人能指出问题所在,我不知道如何解决这个问题将不胜感激。谢谢
【问题讨论】:
-
你真的有一个带有 main 方法的类吗?
-
阅读“入门”指南 (docs.oracle.com/javase/tutorial/getStarted/index.html)。在用 Java 做图形之前,你应该知道一个基本的 hello world 程序的样子以及如何启动它?
-
您正在尝试从命令行运行 Applet。虽然可以这样做,但您需要添加一个 main 方法。小程序不必有 main 方法。
-
@JBNizet 创建一个单独的 HTML 不需要 main 方法就可以解决问题。
-
你应该仍然了解这些基础知识,即使你现在只是在做小程序。
标签: java compiler-errors awt japplet