我想使用init方法绘制一个字符串,但如果在start方法中使用,那么它的工作正常。 请解释我
public class Canv extends Applet //applet class
{
public void start()
{
}
public void init()
{
System.out.println("hi");
Canvas c=new Canvas(); // want to print String in canvas
c.setSize(500,500);
c.setBackground(Color.red);
add(c);
Graphics g=c.getGraphics();
g.drawString("hello buddy",60,60);
}
public void paint(Graphics g)
{
}
public void stop()
{
System.out.println("stop");
}
}
答案 0 :(得分:1)
如果您仔细阅读Java提供的API,那么您会看到init
:
* Called by the browser or applet viewer to inform
* this applet that it has been loaded into the system. It is always
* called before the first time that the <code>start</code> method is
* called.
* <p>
* A subclass of <code>Applet</code> should override this method if
* it has initialization to perform. For example, an applet with
* threads would use the <code>init</code> method to create the
* threads and the <code>destroy</code> method to kill them.
对于start
:
* Called by the browser or applet viewer to inform
* this applet that it should start its execution. It is called after
* the <code>init</code> method and each time the applet is revisited
* in a Web page.
显然,使用此接口操作Java applet的代码不会显示用init()
方法编写的画布,因为此时的UI尚未就绪。容器开始初始化您的应用程序只是一个标记。