What is the life cycle of Java Applet? This article will introduce you to the Applet life cycle and let you know what the five states and their corresponding methods are in the Applet life cycle. I hope it will be helpful to you.
What is the life cycle of a Java Applet?
The applet will go through various states between its object creation and object deletion (when execution ends). This process is called the Applet life cycle. There are 5 states in the applet, and each state is represented by a method. Therefore, there are a total of 5 methods to represent these 5 states respectively (as shown in the figure above). [Video tutorial recommendation: Java Tutorial]
These methods are called "callback methods" because the browser will automatically call them as long as the applet program needs to be executed smoothly; and the programmer just uses Some code writes methods but never calls them.
Applet’s life cycle method
##When executing the applet The method execution sequence
When the applet starts executing, the following methods will be called in the following order:1, init()2, start()3. paint( )When the applet terminates, the following method calls will occur: 1. stop( )2. destroy( )Note: The life cycle of the applet starts with the init() method and ends with the destroy() method; and these two methods will only be executed once, but other methods: start(), paint() and stop() Will be executed multiple times.Let’s take a closer look at these methods:
init() method: It is the first method called and performs variable declaration and initialization operations. The place. start() method: It is used to start the Applet, which contains the actual code of the applet that should be run. It is executed immediately after the init() method; it can also be called when the browser is maximized, restored, or moved from one window to another. paint() method: It is used to redraw the output of the applet display area. It will be executed after executing the start() method and whenever the applet or browser resizes the window. stop() method: used to stop the execution of the applet. It will be executed when the applet stops or the browser is minimized. destroy() method: used to destroy the Applet, it will delete the applet object from the memory. The destroy() method is called after the stop() method.A simple example of the applet life cycle:
The program is as follows:import java.awt.*; import java.applet.*; public class MyApplet extends Applet { public void init() { System.out.println("Applet初始化"); } public void start() { System.out.println("Applet执行开始"); } public void stop() { System.out.println("Applet执行停止"); } public void paint(Graphics g) { System.out.println("绘制输出..."); } public void destroy() { System.out.println("Applet被破坏"); } }Use the appletviewer tool to run the above applet program, then The output is: Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.
The above is the detailed content of What is the life cycle of Java Applet? Introduction to life cycle methods. For more information, please follow other related articles on the PHP Chinese website!