JavaRush /Java Blog /Random EN /Life cycle of a Java applet
AndySkvo
Level 21
Санкт-Петербург

Life cycle of a Java applet

Published in the Random EN group
Life cycle of a Java applet - 1 This listing of a Java applet will help you understand the life cycle of a Java applet. When you run the applet in the Applet Viewer (NetBeans IDE), the methods called will be displayed. import java.applet.Applet; import java.awt.*; public class AndysApplet extends Applet { String output = ""; String event; //init()- метод инициализации апплета public void init() { event = "Вызов метода init() "; printOutput(); } //start()- метод начинает выполнение апплета, либо перезапускает его public void start() { event = "Вызов метода start() "; printOutput(); } //stop()- вызывается при остановке апплета public void stop() { event = "Вызов метода stop() "; printOutput(); } //destroy()- метод завершения действий. Вызывается после метода stop(). //Всегда вызывается последним. public void destroy() { event = "Вызов метода destroy() "; printOutput(); } private void printOutput() { System.out.println(event); output += event; repaint(); } public void paint(Graphics g) { g.drawString(output, 10, 10); } } Since in the operating mode of the Java applet we will not be able to track the call to the stop and destroy methods, we will use the services of the console. Let's launch the applet in Applet Viewer. Applet Viewer allows you to test an applet in various states of its lifecycle. A screenshot of the control menu is shown in Fig. 1. Rice.  1. There is an alternative way of testing: when minimizing the Apllet Viewer window, the stop method will be called; after expanding, the start method will resume the applet's operation. When closing the Java window, the machine will call the stop method, and immediately after it destroy. But this method is less functional.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION