?c BQQMFU?ú)m?? ? ) 38FCí ?  ?? e??3?BQQMFU ) ?"QQMFU ??¥ZE )8FC:?? ?BQQMFU )V)5.-_BQQMFU.?? ? )"QQMJDBUJPO?"QQMFU-WD )) ? ?S?o ? Yq )|+BWB??u3 ?y )Java¥* ù??1B?? appletb )? " -1? [?v?s è0 ^ application?I application¥M1? M ? V[?¨?I applet?b )?c|D ??I Java appletb The Applet Class public class MyApplet extends Applet { public void init() { ... } public void start() { ... } public void stop() { ... } public void destroy() { ... } //your other methods } í ? YV?¨?t ZE ? e? applet  a f ?/?tZ E I 19?S13 ?+?? ?31¨ ??? e ì[Lí ?  ?? ??¨M? ¥} ?b Browser Calling Applet Methods init( ) start( ) destory( ) | stop( ) × é ? Web: Rí?¨: ? 6B:  S?-a The init() Method )?BQ × applet?31?¨ init( )ZEb ) ?T Applet¥0 ? μ S??T , ü ??-? initZEbYè?ZE LC¥ ? ? ?7yL?a m^a !? ¨?? ?Fq[#V HTML?:¥ <applet>S:? |? ?b The start() Method initZE??a startZEü?$?¨bQ` ùc applet¥?: H applet?$? H ???¨ startZEb `ùc applet¥ Web: Ho13?B?8? T Applet0 ?ü??-??BZEb è ? c??¥ appletü31 P¨ startZE ????b The stop() Method stopZED startZE zMQ startZE ^¨? Ríc applet¥?: H?¨¥7 stopZE ^¨ ? ? 7?: H?¨¥b ?c applet¥ Web:? Vn H ?T?μ  e31 3?¥?T Applet0 ?ü??-??BZEb ?¨? ? 7?: appletXü 7 S????¥ ??L ??|??é?b? H?? P¨ stopZE+ ? t??¥L?[L??¥ applet?]¨" d'÷b The destroy() Method í ? ?è| H?Y? applet ?Xü? 31i O?? db ?]μ¥'÷? Hü? ?¨ destroyZEb stopZE9 ^ destoryZE - -?¨b ?T Applet0 ? s?g?- - ??μ313 ?¥?Tü??-??BZEbYè f ?/ ?31-??BZE"d1 db·?¥' ÷ ? applet ?7y¥L??b Example 10.1 Using Applets )' è U9 ?·?~ {¥ appletbo1 { ? ? qa M ??~ {9-? 9 ?? f applet ü?A Uê?-?9ê?-b Run Applet ViewerMortgageApplet ) MortgageApplet P¨ public? e??5 Web í ? íE?¨ ?b )Y?? f Yq31 LC ActionListener¤ gb ) initZE S?¨?? ?b Writing Applets ) Applet ^ AWT¥B? ?? ?D SwingFq?z1x ?yTb applet?1  P¨ SwingFqYV ?Z javax.swing.JApplet ?7y appletbJApplet??  Applet ?¥ ?μZE7 O|?b? SwingFqb  a f ?/ P¨ BorderLayout? 5 ?  . ) ?T31× init(), start(), stop(), and destroy(). ? 8 f ?/?tZE? ^ b¥ . ) V[ JApplet?@F1-¥ ? ?ZE . ) Applets 9 ^ 3 ? HTMLó?? . The <applet> HTML Tag <applet code=classfilename.class width=applet_viewing_width_in_pixels height=applet_viewing_height_in_pixels [archive=archivefile] [codebase=applet_url] [vspace=vertical_margin] [hspace=horizontal_margin] [align=applet_alignment] [alt=alternative_text] > <param name=param_name1 value=param_value1> </applet>  Java Plug-In?? Applets Java Plug-in / ? ? P? ?¥í ?  ?? ü ? ×?1í ? appletb 1X P¨ Java Plug-inA?|?¨ <applet>S:¥ e? HTMLóqD? <object>S:¥M?ˉ¥ HTMLóq V[ P¨ Javasoft 34¥ Java Plug-in HTML Converter LCDb /1? http://java.sun.com/products/plugin/1.3/converter.html How to Use Java Plug-In )| zipóq3a ê-a { ? DOS  7 ü?A UB?4 U3 g·? HTMLó q ?¥óqC . ) ?T?  J2SDK1.4' V°¤í ? ?? JApplet. java –jar htmlconv1_3.jar –gui _ Applet.?? ? <applet code = "DisplayMessage.class" width = 200 height = 50> <param name=MESSAGE value="Welcome to Java"> <param name=X value=20> <param name=Y value=20> alt="You must have a Java-enabled browser to view the applet" </applet> Example 10.2 Passing Parameters to Java Applets )' èIB?·?ê?A U?¥ appletb ?V HTML÷óq?¤??? ê? (x,y)b Run Applet Viewer DisplayMessage )?? initZE? | HTMLóq¥? ?′b¨ getParameterZE¤?? ?′ ^B?3?1b )?M? ?′A U2Tü?? 3M?b Converting Applets into Applications ) JFrame? JApplet ?μtμY?9μ? ]-)b?? ? ì? ^ Container0 ? ?μ¨?? ?Fqa? 5 ? [# ) ? Yq? ^B"¥b Application ^YV java3 d ?¥ 7 applet ^YV Webí ? ?¥b ) AppletD Application¥?? – l  P¨ applet¥ HTML:b ?T appletV HTML |? ?  V  7?) ?b – V JFrame ?¥0 ?? ? 3B?? ?7? ^V JApplet ? ? 3b è ? ? 3B? 1 NewClass¥? ?b – ?¨ init? startZE?¥} ?I? ?¥/ZEb – @FB? mainZEb – ?? applet àμS5 £ V ?¨ setTitleZE13 g@FB ?S5b Example 10.3 Converting Applets into Applications ) Objective: Convert MortgageApplet (from Example 10.1) to a Java application. Run MortgageApplication )?? ?Z  JFrame7? ^ JApplet ?b )?? P¨/ZE S?¨?? ?7d init ZEb )/ZE? ?¨ setTitleZE1 O !?B ?S5b )7y mainZEYV java3 d  ????b Converting Applications into Applets ) ApplicationD Applet¥?? – ?TB?{ <applet>S:¥ HTML:i?S: ??¨ appletb ?T application? P¨   7?? ? * 1 <applet>S:?F ?? ?i initZ E? P¨ getParameterZE |? ?b – V JApplet7? ^V JFrame? ? 3? ?b – ¨ initZE}9 application¥/ZEb – ÷"c7y?A U OZE¥ mainZEb – ?? applet àμS5 £yN ÷ ? setTitleZEb Example 10.4 Converting Applications into Applets ) Objective: Convert a Java application that performs simple numeric calculations (MenuDemo, from Example 9.11) into a Java applet. Run Applet Viewer AppletMenuDemo ) ?Z JApplet7d JFrameb ) [ initZE}9/ZE ÷" setTitleZEb ) í ? ?? ?1> applet P¨ System.exit(0) ??? appletb ) 7yB?? applet¥ HTMLóqb )  AWT???o ?¨? application7 Swing? application? applet? ? P¨??b Example 10.5 ??T1 Applet? Application? ) Objective: ?? è 10.2¥ applet DisplayMessage P ? V[T1 applet? application?b DisplayMessageApp Run as Application Run as Applet )??T1 applet? H1?- { mainZE T1 application? Hü?¨ mainZEb ) MainZE7y B? JFrame` frame?B? JApplet` applet| appletb? O frame? i?¨ ?¥ initZEb ?S Yq ) Java 4 ?SP ¤ g MouseListener and MouseMotionListener) ? ?S Yqb ) MouseListenerSP ?S¥?/a ? 7aé ? a|?? ??1b ) MouseMotionListenerSP ?S¥M??? ???1b Example 10.6 Moving Message Using Mouse )' è ?e?A UBH? V[¨ ?SM? ??b?? ?S H?9 ?-M??9 ^A U ?S·?)b MoveMessageDemo Run as Application Run as Applet Example 10.7 P¨ ?S? )' èZ UB?¨ ?S ?p?¥??b ?e  ?t ?SPoM? ?Sü V[???t·o M?ü V[? ? ??¥êb Scribble Demo Run as Applet Run as Application o ? Yq o1?B/oü??o ? YqbYVo ? Yq V[ ?¨o ? e??3?Bt?TVo ? é? { ?b o ? Yq? KeyListener¤ gSP ) keyPressed(KeyEvent e) Called when a key is pressed. ) keyReleased(KeyEvent e) Called when a key is released. ) keyTyped(KeyEvent e) Called when a key is pressed and then released. The KeyEvent Class ) Methods: getKeyChar() method getKeyCode() method ) Keys: Home VK_HOME End VK_End Page Up VK_PGUP Page Down VK_PGDN etc... Example 10.8 Keyboard Events Demo )' è UB?A U¨? { ?3?¥??b¨? V[ P¨pho ;So a/aPa·M ?3?b KeyboardEventDemo Run as AppletRun as Application ¨?m ¨?mYV Y?ZE?¨¥ HW ¨? ? í ?`- W¥?oT¨b anObject: TheClass Class role Method Invocation Activation anotherObject: TheOtherClass Method Invocation anotherMethod() aMethod() ¨?m ¨?mYV Y?ZE?¨¥ HW ¨? ? í ?`- W¥?oT¨b?/ í íF? anObject: TheClass Class role Method Invocation Activation anotherObject: TheOtherClass Method Invocation anotherMethod() aMethod() ?? ?V U` ?  ¥T¨ ¨?m??¥ `}V ?? ?b ¨?m ¨?mYV Y?ZE?¨¥ HW ¨? ? í ?`- W¥?oT¨b?/ í íF? anObject: TheClass Class role Method Invocation Activation anotherObject: TheOtherClass Method Invocation anotherMethod() aMethod() 3 LV U  HW =` ^i¥B?¨ V`?¥¥<°′L ( ?L )V Ub ¨?m ¨?mYV Y?ZE?¨¥ HW ¨? ? í ?`- W¥?oT¨b?/ í íF? anObject: TheClass Class role Method Invocation Activation anotherObject: TheOtherClass Method Invocation anotherMethod() aMethod() V U`3?B [?T ?¨¥ HWB ?¨ê? 3 L ¥N ?V Ub ¨?m ¨?mYV Y?ZE?¨¥ HW ¨? ? í ?`- W¥?oT¨b?/ í íF? anObject: TheClass Class role Method Invocation Activation anotherObject: TheOtherClass Method Invocation anotherMethod() aMethod() ZE?¨V U `-W¥Y?B ?¨SμZE?¨ ¥ £ üphV Ub Sequence diagrams, cont. : ButtonDemo messagePanel: MessagePanel setXCoordinate repaint  ?m  ?m¨ ? í ?`¥ e? @b Indicate Initial State Transition State1 State2  ?V U` 3i? ù =¥ ?·` ?@ t Hqa3? t?T?? t Yq? 3?b ?? ? ?μB? 3b?? ?V U ?{ph¥ L?l? V U S ?b MV U ? ?-W¥1" V UB?`3? t?TV B? ?M1 6B? ?bB ?¨Sμ? aZE?¨¥ LLp h ?V UMb  ?m í ? Applet¥ e? @ Loaded Initialized init() Started Stopped Destroyed start() stop() stop() destroy() Example 10.9 ?0 3$ ) 3$? ?3$? 3*3?ì¥?íì? } @SS: B? |¨ XB? |¨ Ob ?TB?3$??ì¥ £ üa <°?LZ_ T  ?? ??S:3$ü[??3 $?¤ ;7á?b ??ì¥ ?μ?S ? S:7 àμá 3 a ;?üC  ü b ?? !9?? ) ??¨?? ?A U%?S: X ) P?íì ?Y? ?S? A U%?S: X ) x?3$?[L?9A US: X? O ) ? ^?μ3$? ; ?μ¥?íì?$] ? ˉí ;? ) LC?A U3$? ?M?BQ ?SS & A UY ?? Example 10.9 ?0 3$ Cell -char token +getToken +setToken +paintComponet +mouseClicked 1 9 TicTacToe -char whoseTurn -Cell[][] = new Cell[3][3] -JLabel jlblStatus +init +isFull +isWinning JPanelJApplet MouseListener Run as Application Run as Applet TicTacToe Javay?¥u?3 )Java¥i?óqì T JAR? @?¥ ZIPóqì Tb ) jar -cf TicTacToe.jar TicTacToe.class TicTacToe$Cell.class ) -c:7y?i?óq ) -f:·?i?óq )? H9F appletS:?¥S Mb Advanced Layout (Optional from here on) )CardLayout )GridBagLayout )Using No Layout Manager CardLayout (Optional from here on) ) The CardLayout manager arranges components in a queue of cards. You can only see one card at a time. To construct a CardLayout, simply use the constructor CardLayout(). ) To add a component in the CardLayout container, use the following method: void add(Component com, String name) CardLayout View Components )void first(container) )void last(container) )void next(container) )void previous(container) )void show(Container, String name) Example 10.10 Testing CardLayout Manager ) Objective: Create two panels in a frame. The first panel holds named components. The second panel uses buttons and a choice box to control which component is shown. ShowCardLayout Run GridBagLayout The GridBagLayout manager is the most flexible and the most complex. It is similar to the GridLayout manager in the sense that both layout managers arrange components in a grid. The components can vary in size, however, and can be added in any order in GridBagLayout. Example 10.11: Using GridBagLayout Manager RunShowGridBagLayout Using No Layout Manager You can place components in a container without using any layout manager. In this case, the component must be placed using the component’s instance method setBounds(). Example 10.12: Using No Layout Manager RunShowNoLayout ˉ5 )8"? applet$ ) í ? Applet ?¥ZEb )8"|B?FqF ?? JApplet?$ ) ??V applet?7yB? O$ ) V[| appletb ??B? O? ?$ )3 d applicationD appletoMD¥??b ) ?SM?¥ Yq ?? ^ I 1$ ) P¨ I 1ZEY?M? ?S Yq$ I?  )I applet _?my b