java - 在类里面找不到主要方法

标签 java compiler-errors awt japplet

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)。如果有人指出问题所在,我不知道如何解决此问题。谢谢

最佳答案

您可以在启用了applet的浏览器上运行此Applet。用以下方法启动小程序。

public void init() // This method works as like main.

您只需要在html页面上配置applet标签即可运行此代码。
<applet  code   = "Bounce.class"
    width   = "500"
    height  = "300">
</applet>

关于java - 在类里面找不到主要方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20004104/

相关文章:

java - 如何使用FontMetrics?

java - java中的垂直选框JLabel?

java - 无需注销即可检索注销时间

C++:无法编译 std::tr1::unordered_map

c - 使用MinGW编译C代码,但报错liblto_plugin-0.dll not found?

compiler-errors - Visual Studio 2013无法打开共享项目

java - 带图形的循环 - 生成图形时的错误

java - Spring Security 用户详细信息最佳实践

java - 如果 string.contains() 正确,如何使嵌套循环中断

java - 尝试在 java 中使用 TLSv2 连接到 ssl url 时出现错误 "handshake_failure"