我的代码尽管没有任何缺陷并且完全编译,但由于applet“未初始化”而拒绝显示任何内容。为什么我的程序拒绝输出! 我要求在回答这个问题时,你认为我是业余爱好者。
提前致谢!!!
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.KeyEvent.*;
import java.awt.event.KeyListener.*;
import java.applet.*;
import java.awt.Graphics;
public class Tester extends JApplet implements Runnable
{
// instance variables - replace the example below with your own
private int xCoord;
private int yCoord;
Thing t;
Graphics g;
boolean isRunning = true;
Thread th;
public void init()
{
xCoord = 100;
yCoord = 100;
t = new Thing(xCoord,yCoord);
th = new Thread(this);
setSize(500,500);
t.draw(g,xCoord,yCoord);
}
public void run()
{
while(isRunning)
{
t.update(this);
repaint();
try
{
Thread.sleep(20);
}
catch(Exception e)
{
}
}
}
/**
* Called by the browser or applet viewer to inform this JApplet that it
* should start its execution. It is called after the init method and
* each time the JApplet is revisited in a Web page.
*/
public void start()
{
th.start();
// provide any code requred to run each time
// web page is visited
}
/**
* Called by the browser or applet viewer to inform this JApplet that
* it should stop its execution. It is called when the Web page that
* contains this JApplet has been replaced by another page, and also
* just before the JApplet is to be destroyed.
*/
public void stop()
{
isRunning = false;// provide any code that needs to be run when page
// is replaced by another page or before JApplet is destroyed
}
/**
* Paint method for applet.
*
* @param g the Graphics object for this applet
*/
public void paint(Graphics g)
{
t.paint(g);
g.fillRect(100,100,100,100);
}
/**
* Called by the browser or applet viewer to inform this JApplet that it
* is being reclaimed and that it should destroy any resources that it
* has allocated. The stop method will always be called before destroy.
*/
public void destroy()
{
isRunning=false;// provide code to be run when JApplet is about to be destroyed.
}
}
class Thing
{
private int x;
private int y;
public Thing(int x, int y)
{
this.x = x;
this.y = y;
}
public void draw(Graphics g, int x, int y)
{
g.setColor(Color.black);
g.fillOval(x,y,50,50);
}
public void paint(Graphics g)
{
draw(g,x,y);
}
public void update(Tester te)
{
x+=1;
y-=1;
}
}