Fondamenti di Informatica C Lezione 24: Dentro le applet 22-11-2004 Il commento iniziale /* --------------------------------------------------- This applet creates a few different user interface components and detects when the user interacted with them. Java's classes: Applet (applet) System (lang) Button (awt) Choice (awt) TextField (awt) Checkbox (awt) CheckboxGroup (awt) Label (awt) Event (awt) Custom classes: UIApplet ------------------------------------------------------ */ Lezione 24: Dentro le applet 22-11-2004 2 Lezione 24: Dentro le applet 22-11-2004 1
Importazioni e dichiarazioni import java.awt.*; public class UIApplet extends java.applet.applet { Button Choice TextField button; choice; textfield; Lezione 24: Dentro le applet 22-11-2004 3 Creazione dell interfaccia 1 /** Create a user interface. */ public void init() { Checkbox checkbox; CheckboxGroup checkboxgroup; Label label; // create a choice list choice = new Choice(); choice.additem("apple"); choice.additem("banana"); choice.additem("cherry"); add(choice); Lezione 24: Dentro le applet 22-11-2004 4 Lezione 24: Dentro le applet 22-11-2004 2
Creazione dell interfaccia 2 // create a text field textfield = new TextField(10); // 10 columns wide add(textfield); // create a button button = new Button("Click me"); add(button); // create a label label = new Label("I am a label"); add(label); Lezione 24: Dentro le applet 22-11-2004 5 Creazione dell interfaccia 3 // create 3 exlusive-choice checkboxes checkboxgroup = new CheckboxGroup(); checkbox = new Checkbox("Yes", checkboxgroup, false); add(checkbox); checkbox = new Checkbox("No", checkboxgroup, false); add(checkbox); checkbox = new Checkbox("Maybe", checkboxgroup, true); add(checkbox); Lezione 24: Dentro le applet 22-11-2004 6 Lezione 24: Dentro le applet 22-11-2004 3
Per poterla provare <applet codebase=." code="uiapplet.class" width=256 height=77> </applet> Lezione 24: Dentro le applet 22-11-2004 7 La gestione degli eventi 1 /** Respond to user input events. */ public boolean action(event e, Object arg) { if (e.target == textfield) System.out.println("User entered text into the text field"); button"); choice"); else if (e.target == button) System.out.println("User clicked the else if (e.target == choice) System.out.println("User selected a new Lezione 24: Dentro le applet 22-11-2004 8 Lezione 24: Dentro le applet 22-11-2004 4
La gestione degli eventi 2 box"); else if (e.target instanceof Checkbox) System.out.println("User clicked a check else System.out.println("Unrecognized event"); System.out.println(arg); return super.action(e, arg); Proviamo a compilarla ex novo Lezione 24: Dentro le applet 22-11-2004 9 Esecuzione con tre browser diversi: I.E. Netscape Safari Lezione 24: Dentro le applet 22-11-2004 10 Lezione 24: Dentro le applet 22-11-2004 5
Considerazione ovvia: Finora abbiamo visto applet contenenti tre metodi: init paint action action ovviamente gestisce gli eventi init e paint non sono molto chiare Nessuno di questi viene chiamato direttamente da noi! Perché? Lezione 24: Dentro le applet 22-11-2004 11 Le fasi iniziali Lezione 24: Dentro le applet 22-11-2004 12 Lezione 24: Dentro le applet 22-11-2004 6
Il ciclo di vita Lezione 24: Dentro le applet 22-11-2004 13 Il ciclo di vita, a parole (1) First, an applet is born. This occurs when the applet is loaded into the browser and instantiated. As soon as the applet has been instantiated, the browser invokes the applet s init() method. After the applet has been initialized, the browser starts it going. The browser invokes the applet s start() method. If the user changes to another Web page or to another program, the applet goes to sleep. The browser invokes the applet s stop() method when this occurs. If the user turns back to the original Web page that did contain the applet, the applet wakes up. The browser invokes its start() method again when this occurs. Lezione 24: Dentro le applet 22-11-2004 14 Lezione 24: Dentro le applet 22-11-2004 7
Il ciclo di vita, a parole (2) Finally, at some point when the applet is no longer needed, it goes away. This would occur if the user quit the browser, for example. At this point, the browser invokes the applet s destroy() method. Lezione 24: Dentro le applet 22-11-2004 15 Intervenire nel ciclo di vita... public void init() { public void start() { public void stop() { public void destroy() { Provare, provare subito!!!! [Ciclovita.java] Lezione 24: Dentro le applet 22-11-2004 16 Lezione 24: Dentro le applet 22-11-2004 8
Ulteriori dettagli: init() init(): This method is invoked only once in the applet s life the very first time the user runs the applet. You might want to initialize your applet s user interface by creating windows, buttons, and other graphical elements. For example, the SimpleDraw applet provides an init() method to create the shape and color selection choices. init() is probably the method you ll use the most. Lezione 24: Dentro le applet 22-11-2004 17 Ulteriori dettagli: start() start(): This method is invoked every time the browser starts up your applet. For example, if the user turns to the Web page containing this applet, start() will be invoked. If the user then turns to another page and then turns back, start() will be invoked again. This is different from init(), which is only invoked the very first time. If you are performing any animation or playing any sounds, you might want to start these going inside the start() method. Lezione 24: Dentro le applet 22-11-2004 18 Lezione 24: Dentro le applet 22-11-2004 9
Ulteriori dettagli: stop() stop(): This method is invoked every time the browser stops your applet. There will be one stop() invocation for every start(). You can take this time to halt any animation or sound that you might have begun in start(). Lezione 24: Dentro le applet 22-11-2004 19 Ulteriori dettagli: destroy() destroy(): This method is invoked only once at the very end of an applet s life. There will be one destroy() invocation for every init(). When the browser unloads the applet for example, if the user quits the browser this method will be invoked. You might take this time to free any resources you ve allocated in the system. It s very likely that you ll hardly ever write a destroy() method. Lezione 24: Dentro le applet 22-11-2004 20 Lezione 24: Dentro le applet 22-11-2004 10