Java:Applet 中方法的调用顺序是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/385824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: In what order are the methods called in an Applet?
提问by Devoted
Of all these methods what's being run and in what order?? I guess the first question to ask is whats being run first?
在所有这些方法中,正在运行什么以及以什么顺序运行?我想要问的第一个问题是先运行什么?
And why does th.start() start run()?
为什么 th.start() 启动 run()?
import java.applet.*;
import java.awt.*;
import javax.swing.JFrame;
public class BallApplet extends Applet implements Runnable {
int x_pos = 10;
int y_pos = 100;
int radius = 20;
private Image dbImage;
private Graphics dbG;
public void init() {
// setBackground(Color.BLUE);
}
public void start() {
Thread th = new Thread (this);
th.start();
}
public void stop() {}
public void destroy() {}
public void run() {
// 20 second delay per frame refresh (animation doesn't
// need to be perfectly continuous)
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true) {
x_pos++;
repaint();
try {
Thread.sleep(20);
}
catch (InterruptedException ex) {
System.out.println("Caught!");
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void update(Graphics g) {
// implements double buffering
// drawing on doublebufferImage, note the dbG=dbImage.getGraphics(), so everything dbG.whatever() is
// drawing on the Image's graphics which is later drawn with g.drawImage()
// initialize buffer
if (dbImage == null) {
dbImage = createImage (this.getSize().width, this.getSize().height);
dbG = dbImage.getGraphics();
}
// clear screen in background
dbG.setColor(getBackground()); // gets background color
dbG.fillRect(0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbG.setColor(getForeground());
paint(dbG);
// draw image on the screen
g.drawImage(dbImage, 0, 0, this);
}
public void paint(Graphics g) {
g.setColor(Color.RED);
g.fillOval(x_pos-radius, y_pos-radius, 2*radius, 2*radius);
}
}
回答by Alnitak
The init()and start()methods are invoked first.
该init()和start()方法首先被调用。
That in turn creates a Threadand starts that thread, which causes this class's run()method to be invoked.
这反过来创建一个Thread并启动该线程,这会导致run()调用此类的方法。
The paint()method is invoked by Swing independently in the GUI event handling thread, if Swing detects that the applet needs to be redrawn.
paint()如果 Swing 检测到小程序需要重绘,则该方法在 GUI 事件处理线程中由 Swing 独立调用。
I note that the class's main run()method also repeatedly calls repaint(). That explicitly tells the GUI thread to invoke update().
我注意到该类的 mainrun()方法也反复调用repaint(). 这明确告诉 GUI 线程调用update().
回答by Adeel Ansari
The browser or Applet viewer first calls
浏览器或 Applet 查看器首先调用
- init() method to inform this applet that it has been loaded into the system.
- then after init start() method gets called. For more see the Applet class docs.
- Inside start() method there is a call to th.start(). That means start() the thread execution
- That will cause the run() to get invoked
- init() 方法通知此小程序它已加载到系统中。
- 然后在调用 init start() 方法之后。有关更多信息,请参阅 Applet 类文档。
- 在 start() 方法内部有一个对 th.start() 的调用。这意味着 start() 线程执行
- 这将导致 run() 被调用
回答by coobird
From the Life Cycle of an Appletsection of The Java Tutorials, the Applet's following methods are called in order:
从一个小程序的生命周期的部分Java教程,该Applet的方法如下顺序调用:
init()start()stop()destroy()
init()start()stop()destroy()
In addition, the code implements the Runnableinterface, so the BallApplet's run()method is also executed after a new Thread(here, called th) is run by calling the th.start()method. (Calling the Thread.start()method starts a new thread and calls its run()method.)
此外,该代码实现了Runnable接口,所以BallApplet的run()方法也经过了新的执行Thread(这里称为th)是通过调用运行th.start()方法。(调用该Thread.start()方法会启动一个新线程并调用其run()方法。)
The Defining and Starting a Threadsection from The Java Tutorials has more information on Runnableand Threadand how threads are started in Java.
该定义和启动一个线程从Java教程部分有更多的信息Runnable和Thread和线程是如何在Java中开始。
The run()method contains a call to repaint(), and this is an app-triggered update, it will call the BallApplet's update(Graphics g)method. In addition, the system-triggered repaint will trigger the paint(Graphics g)method.
该run()方法包含对 的调用repaint(),这是应用程序触发的更新,它将调用BallApplet的update(Graphics g)方法。此外,系统触发的重绘会触发该paint(Graphics g)方法。
For more information about repainting in AWT, refer to Painting in AWT and Swing. For information on system- and app-triggered painting, see the section on System-Triggered vs. App-Triggered Painting.
有关在 AWT 中重新绘制的更多信息,请参阅在 AWT 和 Swing 中绘制。有关系统和应用程序触发的绘画的信息,请参阅系统触发与应用程序触发的绘画部分。
回答by Eugene Yokota
See Thread.start().
请参阅Thread.start()。
public void start()
Causes this thread to begin execution; the Java Virtual Machine calls the
runmethod of this thread.The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.
使该线程开始执行;Java 虚拟机调用
run该线程的方法。结果是两个线程并发运行:当前线程(从调用 start 方法返回)和另一个线程(执行其 run 方法)。
多次启动一个线程是不合法的。特别是,线程一旦完成执行就可能不会重新启动。

