我试图做一个圆圈,并将其显示在小程序窗口上。但在运行代码后,它既没有创建任何窗口,也没有显示Circle。我的代码没有显示任何错误。错误在哪里?
package webgame;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;
public class StartingPoint extends Applet implements Runnable {
int x = 0;
int y = 0;
int dx = 2;
int dy = 2;
int radius = 10;
@Override
public void init() {
}
@Override
public void start() {
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
while (true) {
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
//Logger.getLogger(StartingPoint.class.getName()).log(Level.SEVERE, null, e);
e.printStackTrace();
}
}
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void stop() {
}
@Override
public void destroy() {
}
@Override
public void paint(Graphics g) {
g.setColor(Color.CYAN);
g.fillOval(x, y, radius, radius);
}
public static void main(String[] args) {
// TODO code application logic here
}
}
您不需要main方法来执行applet,并且您必须在编译您的类之后创建以下html文件。
<HTML>
<HEAD></HEAD>
<BODY>
<div>
<APPLET CODE="Main.class" WIDTH="500" HEIGHT="500">
</APPLET>
</div>
</BODY>
</HTML>
像这样运行
>appletviewer Main.java
查看此链接