簡體   English   中英

在課堂上找不到主要方法

[英]Main method not found in class

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM