µÚÊ®ÈýÕ ´´½¨´°¿ÚºÍ³ÌÐòƬ
ÔÚJava 1.0ÖУ¬Í¼ÐÎÓû§½Ó¿Ú£¨GUI£©¿â×î³õµÄÉè¼ÆÄ¿±êÊÇÈóÌÐòÔ±¹¹½¨Ò»¸öͨÓõÄGUI£¬Ê¹ÆäÔÚËùÓÐÆ½Ì¨É϶¼ÄÜÕý³£ÏÔʾ¡£
µ«Òź¶µÄÊÇ£¬Õâ¸öÄ¿±ê²¢Î´´ïµ½¡£ÊÂʵÉÏ£¬Java 1.0°æµÄ¡°³éÏóWindows¹¤¾ß°ü¡±£¨AWT£©²úÉúµÄÊÇÔÚ¸÷ϵͳ¿´À´¶¼Í¬ÑùÇ·¼ÑµÄͼÐÎÓû§½Ó¿Ú¡£³ý´ËÖ®Í⣬Ëü»¹ÏÞÖÆÎÒÃÇÖ»ÄÜʹÓÃËÄÖÖ×ÖÌ壬²¢ÇÒ²»ÄÜ·ÃÎʲÙ×÷ϵͳÖÐÏÖÓеĸ߼¶GUIÔªËØ¡£Í¬Ê±£¬Jave1.0°æµÄAWT±à³ÌÄ£ÐÍÒ²²»ÊÇÃæÏò¶ÔÏóµÄ£¬¼«²»³ÉÊì¡£ÕâÀàÇé¿öÔÚJava1.1°æµÄAWTʼþÄ£ÐÍÖеõ½Á˺ܺõĸĽø£¬ÀýÈ磺¸ü¼ÓÇåÎú¡¢ÃæÏò¶ÔÏóµÄ±à³Ì¡¢×ñÑJava
BeansµÄ·¶Àý£¬ÒÔ¼°Ò»¸ö¿ÉÇáËÉ´´½¨¿ÉÊÓ±à³Ì»·¾³µÄ±à³Ì×é¼þÄ£ÐÍ¡£Java1.2ΪÀϵÄJava
1.0 AWTÌí¼ÓÁËJava»ù´¡ÀࣨAWT£©£¬ÕâÊÇÒ»¸ö±»³ÆÎª¡°Swing¡±µÄGUIµÄÒ»²¿·Ö¡£·á¸»µÄ¡¢Ò×ÓÚʹÓúÍÀí½âµÄJava
BeansÄܾ¹ýÍϷŲÙ×÷£¨ÏñÊÖ¹¤±à³ÌÒ»ÑùµÄºÃ£©£¬´´½¨³öÄÜʹ³ÌÐòÔ±ÂúÒâµÄGUI¡£Èí¼þÒµµÄ¡°3´ÎÐÞ¶©°æ¡±¹æÔò¿´À´¶ÔÓÚ³ÌÐòÉè¼ÆÓïÑÔÒ²ÊdzÉÁ¢µÄ£¨Ò»¸ö²úÆ·³ý·Ç¾¹ýµÚ3´ÎÐÞ¶©£¬·ñÔò²»»á¾¡ÈçÈËÒ⣩¡£
JavaµÄÖ÷ÒªÉè¼ÆÄ¿µÄÖ®Ò»Êǽ¨Á¢³ÌÐòƬ£¬Ò²¾ÍÊǽ¨Á¢ÔËÐÐÔÚWEB
ä¯ÀÀÆ÷ÉϵÄСӦÓóÌÐò¡£ÓÉÓÚËüÃDZØÐëÊǰ²È«µÄ£¬ËùÒÔ³ÌÐòƬÔÚÔËÐÐʱ±ØÐë¼ÓÒÔÏÞÖÆ¡£ÎÞÂÛÔõÑù£¬ËüÃǶ¼ÊÇÖ§³Ö¿Í»§¶Ë±à³ÌµÄÇ¿ÓÐÁ¦µÄ¹¤¾ß£¬Ò»¸öÖØÒªµÄÓ¦ÓñãÊÇÔÚWebÉÏ¡£
ÔÚÒ»¸ö³ÌÐòƬÖбà³Ì»áÊܵ½ºÜ¶àµÄÏÞÖÆ£¬ÎÒÃÇÒ»°ã˵Ëü¡°ÔÚɳÏäÄÚ¡±£¬ÕâÊÇÓÉÓÚJavaÔËÐÐʱһֱ»áÓÐij¸ö¶«Î÷¡ª¡ª¼´JavaÔËÐÐÆÚ°²È«ÏµÍ³¡ª¡ªÔÚ¼àÊÓ×ÅÎÒÃÇ¡£Jave
1.1Ϊ³ÌÐòƬÌṩÁËÊý×ÖÇ©Ãû£¬ËùÒÔ¿ÉÑ¡³öÄÜÐÅÀµµÄ³ÌÐòƬȥ·ÃÎÊÖ÷»ú¡£²»¹ý£¬ÎÒÃÇÒ²ÄÜÌø³öɳÏäµÄÏÞÖÆÐ´³ö¿É¿¿µÄ³ÌÐò¡£ÔÚÕâÖÖÇé¿öÏ£¬ÎÒÃǿɷÃÎʲÙ×÷ϵͳÖÐµÄÆäËû¹¦ÄÜ¡£ÔÚÕâ±¾ÊéÖÐÎÒÃÇ×ÔʼÖÁÖÕ±àдµÄ¶¼Êǿɿ¿µÄ³ÌÐò£¬µ«ËüÃdzÉΪÁËûÓÐͼÐÎ×é¼þµÄ¿ØÖÆÌ¨³ÌÐò¡£AWTÒ²ÄÜÓÃÀ´Îª¿É¿¿µÄ³ÌÐò½¨Á¢GUI½Ó¿Ú¡£
ÔÚÕâÒ»ÕÂÖÐÎÒÃǽ«ÏÈѧϰʹÓÃÀϵÄAWT¹¤¾ß£¬ÎÒÃÇ»áÓëÐí¶àÖ§³ÖºÍʹÓÃAWTµÄ´úÂë³ÌÐòÑù±¾ÏàÓö¡£¾¡¹ÜÕâÓÐһЩÀ§ÄÑ£¬µ«È´ÊDZØÐëµÄ£¬ÒòΪÎÒÃDZØÐëÓÃÀϵÄAWTÀ´Î¬»¤ºÍÔĶÁ´«Í³µÄJava´úÂë¡£ÓÐʱÉõÖÁÐèÒªÎÒÃDZàдAWT´úÂëÈ¥Ö§³Ö²»ÄÜ´ÓJava1.0Éý¼¶µÄ»·¾³¡£ÔÚ±¾Õµڶþ²¿·Ö£¬ÎÒÃǽ«Ñ§Ï°Java
1.1°æÖÐеÄAWT½á¹¹²¢»á¿´µ½ËüµÄʼþÄ£ÐÍÊÇÈç´ËµÄÓÅÐ㣨Èç¹ûÄÜÕÆÎյϰ£¬ÄÇôÔÚ±àÖÆÐµijÌÐòʱ¾Í¿ÉʹÓÃÕâ×îÐµĹ¤¾ß¡£×îºó£¬ÎÒÃǽ«Ñ§Ï°ÐµÄÄÜÏñÀà¿âÒ»Ñù¼ÓÈëµ½Java
1.1°æÖеÄJFC/Swing×é¼þ£¬ÕâÒâζ×Ų»ÐèÒªÉý¼¶µ½Java 1.2±ãÄÜʹÓÃÕâÒ»Àà¿â¡£
´ó¶àÊýµÄÀý³Ì¶¼½«Õ¹Ê¾³ÌÐòƬµÄ½¨Á¢£¬Õâ²¢²»½ö½öÊÇÒòΪÕâ·Ç³£µÄÈÝÒ×£¬¸üÒòΪÕâÊÇAWTµÄÖ÷Òª×÷Óá£ÁíÍ⣬µ±ÓÃAWT´´½¨Ò»¸ö¿É¿¿µÄ³ÌÐòʱ£¬ÎÒÃǽ«¿´µ½´¦Àí³ÌÐòµÄ²»Í¬Ö®´¦£¬ÒÔ¼°ÔõÑù´´½¨ÄÜÔÚÃüÁîÐкÍä¯ÀÀÆ÷ÖÐÔËÐеijÌÐò¡£
Çë×¢ÒâµÄÊÇÕâ²»ÊÇΪÁËÃèÊöÀàµÄËùÓгÌÐòµÄ×ۺϽâÊÍ¡£ÕâÒ»Õ½«´øÁìÎÒÃÇ´ÓÕªÒª¿ªÊ¼¡£µ±ÎÒÃDzéÕÒ¸ü¸´ÔÓµÄÄÚÈÝʱ£¬ÇëÈ·¶¨ÎÒÃǵÄÐÅÏ¢ä¯ÀÀÆ÷ͨ¹ý²éÕÒÀàºÍ·½·¨À´½â¾ö±à³ÌÖеÄÎÊÌ⣨Èç¹ûÎÒÃÇÕýÔÚʹÓÃÒ»¸ö¿ª·¢»·¾³£¬ÐÅÏ¢ä¯ÀÀÆ÷Ò²ÐíÊÇÄÚ½¨µÄ£»Èç¹ûÎÒÃÇʹÓõÄÊÇSUN¹«Ë¾µÄJDKÔòÕâʱÎÒÃÇҪʹÓÃWEBä¯ÀÀÆ÷²¢ÔÚJava¸ùĿ¼ÏÂÃæ¿ªÊ¼£©¡£¸½Â¼FÁгöÁËÓÃÓÚÉîÈëѧϰ¿â֪ʶµÄÆäËûһЩ²Î¿¼×ÊÁÏ¡£
13.1 ΪºÎÒªÓÃAWT£¿
¶ÔÓÚ±¾ÕÂҪѧϰµÄ¡°ÀÏʽ¡±AWT£¬Ëü×îÑÏÖØµÄȱµã¾ÍÊÇËüÎÞÂÛÔÚÃæÏò¶ÔÏóÉè¼Æ·½Ã棬»¹ÊÇÔÚGUI¿ª·¢°üÉè¼Æ·½Ã棬¶¼Óв»¾¡ÈçÈËÒâµÄ±íÏÖ¡£ËüʹÎÒÃǻص½Á˳ÌÐòÉè¼ÆµÄºÚ°µÄê´ú£¨»»³ÉÆäËû»°¾ÍÊÇ¡°×¾Áӵġ±¡¢¡°¿Éŵġ±¡¢¡°¶ñÁӵġ±µÈµÈ£©¡£±ØÐëΪִÐÐÿһ¸öʼþ±àд´úÂ룬°üÀ¨ÔÚÆäËû»·¾³ÖÐÀûÓá°×ÊÔ´¡±¼´¿ÉÇáËÉÍê³ÉµÄһЩÈÎÎñ¡£
Ðí¶àÏóÕâÑùµÄÎÊÌâÔÚJava 1.1Àï¶¼µÃµ½ÁË»º½â»òÅųý£¬ÒòΪ£º
(1)Java 1.1µÄÐÂÐÍAWTÊÇÒ»¸ö¸üºÃµÄ±à³ÌÄ£ÐÍ£¬²¢Ïò¸üºÃµÄ¿âÉè¼ÆÂõ³öÁË¿ÉϲµÄÒ»²½¡£¶øJava
BeansÔòÊÇÄǸö¿âµÄ¿ò¼Ü¡£
(2)¡°GUI¹¹½¨Æ÷¡±£¨¿ÉÊÓ±à³Ì»·¾³£©½«ÊÊÓÃÓÚËùÓпª·¢ÏµÍ³¡£ÔÚÎÒÃÇÓÃͼÐλ¯¹¤¾ß½«×é¼þÖÃÈë´°ÌåµÄʱºò£¬Java
BeansºÍеÄAWTʹGUI¹¹½¨Æ÷ÄܰïÎÒÃÇ×Ô¶¯Íê³É´úÂë¡£ÆäËü×é¼þ¼¼ÊõÈçActiveXµÈÒ²½«ÒÔÏàͬµÄÐÎʽ֧³Ö¡£
¼ÈÈ»Èç´Ë£¬ÎªÊ²Ã´»¹ÒªÑ§Ï°Ê¹ÓÃÀϵÄAWTÄØ£¿ÔÒòºÜ¼òµ¥£¬ÒòΪËüµÄ´æÔÚÊǸöÊÂʵ¡£¾ÍĿǰÀ´Ëµ£¬Õâ¸öÊÂʵ¶ÔÎÒÃÇÀ´ËµÏÔµÃÓÐЩ²»Àû£¬ËüÉæ¼°µ½ÃæÏò¶ÔÏó¿âÉè¼ÆµÄÒ»¸ö×ÚÖ¼£ºÒ»µ©ÎÒÃÇÔÚ¿âÖй«²¼Ò»¸ö×é¼þ£¬¾ÍÔÙ²»ÄÜÈ¥µôËü¡£ÈçÈ¥µôËü£¬¾Í»áË𺦱ðÈËÒÑ´æÔڵĴúÂë¡£ÁíÍ⣬µ±ÎÒÃÇѧϰJavaºÍËùÓÐʹÓÃÀÏAWTµÄ³ÌÐòʱ£¬»á·¢ÏÖÓÐÐí¶àÔÀ´µÄ´úÂëʹÓõͼÊÇÀÏʽAWT¡£
AWT±ØÐëÄÜÓë¹ÌÓвÙ×÷ϵͳµÄGUI×é¼þ´ò½»Í¨£¬ÕâÒâζ×ÅËüÐèÒªÖ´ÐÐÒ»¸ö³ÌÐòƬ²»¿ÉÄÜ×öµ½µÄÈÎÎñ¡£Ò»¸ö²»±»ÐÅÈεijÌÐòƬÔÚ²Ù×÷ϵͳÖв»ÄÜ×÷³öÈκÎÖ±½Óµ÷Ó㬷ñÔòËü»á¶ÔÓû§µÄ»úÆ÷×ö³ö²»Ç¡µ±µÄÊÂÇé¡£Ò»¸ö²»±»ÐÅÈεijÌÐòƬ²»ÄÜ·ÃÎÊÖØÒªµÄ¹¦ÄÜ¡£ÀýÈ磬¡°ÔÚÆÁÄ»Éϻһ¸ö´°¿Ú¡±µÄΨһ·½·¨ÊÇͨ¹ýµ÷ÓÃÓµÓÐÌØÊâ½Ó¿ÚºÍ°²È«¼ì²éµÄ±ê×¼Java¿â¡£Sun¹«Ë¾µÄÔʼģÐÍ´´½¨µÄÐÅÈο⽫½ö½ö¹©¸øWebä¯ÀÀÆ÷ÖеÄJavaϵͳÐÅÈιØÏµ×Ô¶¯ÊÚȨÆ÷ʹÓã¬×Ô¶¯ÊÚȨÆ÷½«¿ØÖÆÔõÑù½øÈëµ½¿âÖÐÈ¥¡£
µ«µ±ÎÒÃÇÏëÔö¼Ó²Ù×÷ϵͳÖзÃÎÊÐÂ×é¼þµÄ¹¦ÄÜʱ¸ÃÔõô°ì£¿µÈ´ýSunÀ´¾ö¶¨ÎÒÃǵÄÀ©Õ¹±»ºÏ²¢µ½±ê×¼µÄJava¿âÖУ¬µ«Õâ²»Ò»¶¨»á½â¾öÎÒÃǵÄÎÊÌâ¡£Java
1.1°æÖеÄÐÂÄ£ÐÍÊÇ¡°ÐÅÈδúÂ롱»ò¡°Ç©Ãû´úÂ롱£¬Òò´ËÒ»¸öÌØÊâ·þÎñÆ÷½«Ð£ÑéÎÒÃÇÏÂÔØµÄ¡¢Óɹ涨µÄ¿ª·¢ÕßʹÓõĹ«¹²ÃÜÔ¿¼ÓÃÜϵͳµÄ´úÂë¡£ÕâÑùÎÒÃǾͿÉÖªµÀ´úÂë´ÓºÎ¶øÀ´£¬ÄÇÕæµÄÊÇBobµÄ´úÂ룬»¹ÊÇÓÉijÈËαװ³ÉBobµÄ´úÂë¡£Õâ²¢²»ÄÜ×èÖ¹Bob·¸´íÎó»ò×÷ijЩ¶ñÒâµÄÊ£¬µ«ÄÜ·ÀÖ¹BobÌÓ±ÜÄäÃûÖÆÔì¼ÆËã»ú²¡¶¾µÄÔðÈΡ£Ò»¸öÊý×ÖÇ©ÃûµÄ³ÌÐòƬ¡ª¡ª¡°±»ÐÅÈεijÌÐòƬ¡±¡ª¡ªÔÚJava
1.1°æÄܽøÈëÎÒÃǵĻúÆ÷²¢Ö±½Ó¿ØÖÆËü£¬ÕýÏñһЩÆäËüµÄÓ¦ÓóÌÐò´ÓÐÅÈιØÏµ×Ô¶¯ÊÚȨ»úÖеõ½¡°ÐÅÈΡ±²¢°²×°ÔÚÎÒÃǵĻúÆ÷ÉÏ¡£
ÕâÊÇÀÏAWTµÄËùÓÐÌØµã¡£ÀϵÄAWT´úÂ뽫һֱ´æÔÚ£¬ÐµÄJava±à³ÌÕßÔڴӾɵÄÊé±¾ÖÐѧϰʱ½«»áÓöµ½ÀϵÄAWT´úÂ롣ͬÑù£¬ÀϵÄAWTÒ²ÊÇÖµµÃȥѧϰµÄ£¬ÀýÈçÔÚÒ»¸öÖ»ÓÐÉÙÁ¿¿âµÄÀý³ÌÉè¼ÆÖС£ÀϵÄAWTËù°üÀ¨µÄ·¶Î§ÔÚ²»¿¼ÂÇÉî¶ÈºÍö¾Ùÿһ¸ö³ÌÐòºÍÀ࣬ȡ¶ø´úÖ®µÄÊǸøÁËÎÒÃÇÒ»¸öÀÏAWTÉè¼ÆµÄ¸Åò¡£
13.2 »ù±¾³ÌÐòƬ
¿âͨ³£°´ÕÕËüÃǵŦÄÜÀ´½øÐÐ×éºÏ¡£Ò»Ð©¿â£¬ÀýÈçʹÓùýµÄ£¬±ãÖжϸéÖÃÆðÀ´¡£±ê×¼µÄJava¿â×Ö·û´®ºÍʸÁ¿Àà¾ÍÊÇÕâÑùµÄÒ»¸öÀý×Ó¡£ÆäËûµÄ¿â±»ÌØÊâµØÉè¼Æ£¬ÀýÈç¹¹½¨¿éÈ¥½¨Á¢ÆäËüµÄ¿â¡£¿âÖеÄijЩÀàÊÇÓ¦ÓóÌÐòµÄ¿ò¼Ü£¬ÆäÄ¿µÄÊÇÐÖúÎÒÃǹ¹½¨Ó¦ÓóÌÐò£¬ÔÚÌṩÀà»òÀ༯µÄÇé¿öϲúÉúÿ¸öÌØ¶¨Ó¦ÓóÌÐòµÄ»ù±¾»î¶¯×´¿ö¡£È»ºó£¬ÎªÎÒÃǶ¨Öƻ״¿ö£¬±ØÐë¼Ì³ÐÓ¦ÓóÌÐòÀಢÇÒ·ÏÆú³ÌÐòµÄÈ¨Òæ¡£Ó¦ÓóÌÐò¿ò¼ÜµÄĬÈÏ¿ØÖƽṹ½«ÔÚÌØ¶¨µÄʱ¼äµ÷ÓÃÎÒÃÇ·ÏÆúµÄ³ÌÐò¡£Ó¦ÓóÌÐòµÄ¿ò¼ÜÊÇ¡°·ÖÀë¡¢¸Ä±äºÍÖÐֹʼþ¡±µÄºÃÀý×Ó£¬ÒòΪËü×ÜÊÇŬÁ¦È¥³¢ÊÔ¼¯ÖÐÔÚ±»·ÏÆúµÄËùÓÐÌØÊâ³ÌÐò¶Î¡£
³ÌÐòƬÀûÓÃÓ¦ÓóÌÐò¿ò¼ÜÀ´½¨Á¢¡£ÎÒÃÇ´ÓÀàÖм̳гÌÐòƬ£¬²¢ÇÒ·ÏÆúÌØ¶¨µÄ³ÌÐò¡£´ó¶àÊýʱ¼äÎÒÃDZØÐ뿼ÂÇһЩ²»µÃ²»ÔËÐеÄʹ³ÌÐòƬÔÚWEBÒ³ÃæÉϽ¨Á¢ºÍʹÓõÄÖØÒª·½·¨¡£ÕâЩ·½·¨ÊÇ£º
|
Method |
Operation |
|---|---|
|
init( ) |
Called when the applet is first
created to perform first-time initialization of the applet |
|
start( ) |
Called every time the applet moves
into sight on the Web browser to allow the applet to start up its normal
operations (especially those that are shut off by stop( )). Also
called after init( ). |
|
paint( ) |
Part of the base class
Component (three levels of inheritance up). Called as part of an
update( ) to perform special painting on the canvas of an
applet. |
|
stop( ) |
Called every time the applet moves
out of sight on the Web browser to allow the applet to shut off expensive
operations. Also called right before destroy( ). |
|
destroy( ) |
Called when the applet is being
unloaded from the page to perform final release of resources when the applet is
no longer used |
//: Applet1.java
// Very simple applet
package c13;
import java.awt.*;
import java.applet.*;
public class Applet1 extends Applet {
public void paint(Graphics g) {
g.drawString("First applet", 10, 10);
}
} ///:~
<applet code=Applet1 width=200 height=200> </applet>
//: Applet2.java
// Easy graphics
import java.awt.*;
import java.applet.*;
public class Applet2 extends Applet {
public void paint(Graphics g) {
g.drawString("Second applet", 10, 15);
g.draw3DRect(0, 0, 100, 20, true);
}
} ///:~
//: Applet3.java
// Shows init(), start() and stop() activities
import java.awt.*;
import java.applet.*;
public class Applet3 extends Applet {
String s;
int inits = 0;
int starts = 0;
int stops = 0;
public void init() { inits++; }
public void start() { starts++; }
public void stop() { stops++; }
public void paint(Graphics g) {
s = "inits: " + inits +
", starts: " + starts +
", stops: " + stops;
g.drawString(s, 10, 10);
}
} ///:~
//: Button1.java
// Putting buttons on an applet
import java.awt.*;
import java.applet.*;
public class Button1 extends Applet {
Button
b1 = new Button("Button 1"),
b2 = new Button("Button 2");
public void init() {
add(b1);
add(b2);
}
} ///:~
//: Button2.java
// Capturing button presses
import java.awt.*;
import java.applet.*;
public class Button2 extends Applet {
Button
b1 = new Button("Button 1"),
b2 = new Button("Button 2");
public void init() {
add(b1);
add(b2);
}
public boolean action(Event evt, Object arg) {
if(evt.target.equals(b1))
getAppletContext().showStatus("Button 1");
else if(evt.target.equals(b2))
getAppletContext().showStatus("Button 2");
// Let the base class handle it:
else
return super.action(evt, arg);
return true; // We've handled it here
}
} ///:~
//: Button3.java
// Matching events on button text
import java.awt.*;
import java.applet.*;
public class Button3 extends Applet {
Button
b1 = new Button("Button 1"),
b2 = new Button("Button 2");
public void init() {
add(b1);
add(b2);
}
public boolean action (Event evt, Object arg) {
if(arg.equals("Button 1"))
getAppletContext().showStatus("Button 1");
else if(arg.equals("Button 2"))
getAppletContext().showStatus("Button 2");
// Let the base class handle it:
else
return super.action(evt, arg);
return true; // We've handled it here
}
} ///:~
//: TextField1.java
// Using the text field control
import java.awt.*;
import java.applet.*;
public class TextField1 extends Applet {
Button
b1 = new Button("Get Text"),
b2 = new Button("Set Text");
TextField
t = new TextField("Starting text", 30);
String s = new String();
public void init() {
add(b1);
add(b2);
add(t);
}
public boolean action (Event evt, Object arg) {
if(evt.target.equals(b1)) {
getAppletContext().showStatus(t.getText());
s = t.getSelectedText();
if(s.length() == 0) s = t.getText();
t.setEditable(true);
}
else if(evt.target.equals(b2)) {
t.setText("Inserted by Button 2: " + s);
t.setEditable(false);
}
// Let the base class handle it:
else
return super.action(evt, arg);
return true; // We've handled it here
}
} ///:~
//: TextArea1.java
// Using the text area control
import java.awt.*;
import java.applet.*;
public class TextArea1 extends Applet {
Button b1 = new Button("Text Area 1");
Button b2 = new Button("Text Area 2");
Button b3 = new Button("Replace Text");
Button b4 = new Button("Insert Text");
TextArea t1 = new TextArea("t1", 1, 30);
TextArea t2 = new TextArea("t2", 4, 30);
public void init() {
add(b1);
add(t1);
add(b2);
add(t2);
add(b3);
add(b4);
}
public boolean action (Event evt, Object arg) {
if(evt.target.equals(b1))
getAppletContext().showStatus(t1.getText());
else if(evt.target.equals(b2)) {
t2.setText("Inserted by Button 2");
t2.appendText(": " + t1.getText());
getAppletContext().showStatus(t2.getText());
}
else if(evt.target.equals(b3)) {
String s = " Replacement ";
t2.replaceText(s, 3, 3 + s.length());
}
else if(evt.target.equals(b4))
t2.insertText(" Inserted ", 10);
// Let the base class handle it:
else
return super.action(evt, arg);
return true; // We've handled it here
}
} ///:~
//: Label1.java
// Using labels
import java.awt.*;
import java.applet.*;
public class Label1 extends Applet {
TextField t1 = new TextField("t1", 10);
Label labl1 = new Label("TextField t1");
Label labl2 = new Label(" ");
Label labl3 = new Label(" ",
Label.RIGHT);
Button b1 = new Button("Test 1");
Button b2 = new Button("Test 2");
public void init() {
add(labl1); add(t1);
add(b1); add(labl2);
add(b2); add(labl3);
}
public boolean action (Event evt, Object arg) {
if(evt.target.equals(b1))
labl2.setText("Text set into Label");
else if(evt.target.equals(b2)) {
if(labl3.getText().trim().length() == 0)
labl3.setText("labl3");
if(labl3.getAlignment() == Label.LEFT)
labl3.setAlignment(Label.CENTER);
else if(labl3.getAlignment()==Label.CENTER)
labl3.setAlignment(Label.RIGHT);
else if(labl3.getAlignment() == Label.RIGHT)
labl3.setAlignment(Label.LEFT);
}
else
return super.action(evt, arg);
return true;
}
} ///:~
//: CheckBox1.java
// Using check boxes
import java.awt.*;
import java.applet.*;
public class CheckBox1 extends Applet {
TextArea t = new TextArea(6, 20);
Checkbox cb1 = new Checkbox("Check Box 1");
Checkbox cb2 = new Checkbox("Check Box 2");
Checkbox cb3 = new Checkbox("Check Box 3");
public void init() {
add(t); add(cb1); add(cb2); add(cb3);
}
public boolean action (Event evt, Object arg) {
if(evt.target.equals(cb1))
trace("1", cb1.getState());
else if(evt.target.equals(cb2))
trace("2", cb2.getState());
else if(evt.target.equals(cb3))
trace("3", cb3.getState());
else
return super.action(evt, arg);
return true;
}
void trace(String b, boolean state) {
if(state)
t.appendText("Box " + b + " Set\n");
else
t.appendText("Box " + b + " Cleared\n");
}
} ///:~
//: RadioButton1.java
// Using radio buttons
import java.awt.*;
import java.applet.*;
public class RadioButton1 extends Applet {
TextField t =
new TextField("Radio button 2", 30);
CheckboxGroup g = new CheckboxGroup();
Checkbox
cb1 = new Checkbox("one", g, false),
cb2 = new Checkbox("two", g, true),
cb3 = new Checkbox("three", g, false);
public void init() {
t.setEditable(false);
add(t);
add(cb1); add(cb2); add(cb3);
}
public boolean action (Event evt, Object arg) {
if(evt.target.equals(cb1))
t.setText("Radio button 1");
else if(evt.target.equals(cb2))
t.setText("Radio button 2");
else if(evt.target.equals(cb3))
t.setText("Radio button 3");
else
return super.action(evt, arg);
return true;
}
} ///:~
//: Choice1.java
// Using drop-down lists
import java.awt.*;
import java.applet.*;
public class Choice1 extends Applet {
String[] description = { "Ebullient", "Obtuse",
"Recalcitrant", "Brilliant", "Somnescent",
"Timorous", "Florid", "Putrescent" };
TextField t = new TextField(30);
Choice c = new Choice();
Button b = new Button("Add items");
int count = 0;
public void init() {
t.setEditable(false);
for(int i = 0; i < 4; i++)
c.addItem(description[count++]);
add(t);
add(c);
add(b);
}
public boolean action (Event evt, Object arg) {
if(evt.target.equals(c))
t.setText("index: " + c.getSelectedIndex()
+ " " + (String)arg);
else if(evt.target.equals(b)) {
if(count < description.length)
c.addItem(description[count++]);
}
else
return super.action(evt, arg);
return true;
}
} ///:~
//: List1.java
// Using lists with action()
import java.awt.*;
import java.applet.*;
public class List1 extends Applet {
String[] flavors = { "Chocolate", "Strawberry",
"Vanilla Fudge Swirl", "Mint Chip",
"Mocha Almond Fudge", "Rum Raisin",
"Praline Cream", "Mud Pie" };
// Show 6 items, allow multiple selection:
List lst = new List(6, true);
TextArea t = new TextArea(flavors.length, 30);
Button b = new Button("test");
int count = 0;
public void init() {
t.setEditable(false);
for(int i = 0; i < 4; i++)
lst.addItem(flavors[count++]);
add(t);
add(lst);
add(b);
}
public boolean action (Event evt, Object arg) {
if(evt.target.equals(lst)) {
t.setText("");
String[] items = lst.getSelectedItems();
for(int i = 0; i < items.length; i++)
t.appendText(items[i] + "\n");
}
else if(evt.target.equals(b)) {
if(count < flavors.length)
lst.addItem(flavors[count++], 0);
}
else
return super.action(evt, arg);
return true;
}
} ///:~
//: List2.java
// Using lists with handleEvent()
import java.awt.*;
import java.applet.*;
public class List2 extends Applet {
String[] flavors = { "Chocolate", "Strawberry",
"Vanilla Fudge Swirl", "Mint Chip",
"Mocha Almond Fudge", "Rum Raisin",
"Praline Cream", "Mud Pie" };
// Show 6 items, allow multiple selection:
List lst = new List(6, true);
TextArea t = new TextArea(flavors.length, 30);
Button b = new Button("test");
int count = 0;
public void init() {
t.setEditable(false);
for(int i = 0; i < 4; i++)
lst.addItem(flavors[count++]);
add(t);
add(lst);
add(b);
}
public boolean handleEvent(Event evt) {
if(evt.id == Event.LIST_SELECT ||
evt.id == Event.LIST_DESELECT) {
if(evt.target.equals(lst)) {
t.setText("");
String[] items = lst.getSelectedItems();
for(int i = 0; i < items.length; i++)
t.appendText(items[i] + "\n");
}
else
return super.handleEvent(evt);
}
else
return super.handleEvent(evt);
return true;
}
public boolean action(Event evt, Object arg) {
if(evt.target.equals(b)) {
if(count < flavors.length)
lst.addItem(flavors[count++], 0);
}
else
return super.action(evt, arg);
return true;
}
} ///:~
//: FlowLayout1.java
// Demonstrating the FlowLayout
import java.awt.*;
import java.applet.*;
public class FlowLayout1 extends Applet {
public void init() {
setLayout(new FlowLayout());
for(int i = 0; i < 20; i++)
add(new Button("Button " + i));
}
} ///:~
//: BorderLayout1.java
// Demonstrating the BorderLayout
import java.awt.*;
import java.applet.*;
public class BorderLayout1 extends Applet {
public void init() {
int i = 0;
setLayout(new BorderLayout());
add("North", new Button("Button " + i++));
add("South", new Button("Button " + i++));
add("East", new Button("Button " + i++));
add("West", new Button("Button " + i++));
add("Center", new Button("Button " + i++));
}
} ///:~
//: GridLayout1.java
// Demonstrating the GridLayout
import java.awt.*;
import java.applet.*;
public class GridLayout1 extends Applet {
public void init() {
setLayout(new GridLayout(7,3));
for(int i = 0; i < 20; i++)
add(new Button("Button " + i));
}
} ///:~
//: CardLayout1.java
// Demonstrating the CardLayout
import java.awt.*;
import java.applet.Applet;
class ButtonPanel extends Panel {
ButtonPanel(String id) {
setLayout(new BorderLayout());
add("Center", new Button(id));
}
}
public class CardLayout1 extends Applet {
Button
first = new Button("First"),
second = new Button("Second"),
third = new Button("Third");
Panel cards = new Panel();
CardLayout cl = new CardLayout();
public void init() {
setLayout(new BorderLayout());
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.add(first);
p.add(second);
p.add(third);
add("North", p);
cards.setLayout(cl);
cards.add("First card",
new ButtonPanel("The first one"));
cards.add("Second card",
new ButtonPanel("The second one"));
cards.add("Third card",
new ButtonPanel("The third one"));
add("Center", cards);
}
public boolean action(Event evt, Object arg) {
if (evt.target.equals(first)) {
cl.first(cards);
}
else if (evt.target.equals(second)) {
cl.first(cards);
cl.next(cards);
}
else if (evt.target.equals(third)) {
cl.last(cards);
}
else
return super.action(evt, arg);
return true;
}
} ///:~
//: AutoEvent.java
// Alternatives to action()
import java.awt.*;
import java.applet.*;
import java.util.*;
class MyButton extends Canvas {
AutoEvent parent;
Color color;
String label;
MyButton(AutoEvent parent,
Color color, String label) {
this.label = label;
this.parent = parent;
this.color = color;
}
public void paint(Graphics g) {
g.setColor(color);
int rnd = 30;
g.fillRoundRect(0, 0, size().width,
size().height, rnd, rnd);
g.setColor(Color.black);
g.drawRoundRect(0, 0, size().width,
size().height, rnd, rnd);
FontMetrics fm = g.getFontMetrics();
int width = fm.stringWidth(label);
int height = fm.getHeight();
int ascent = fm.getAscent();
int leading = fm.getLeading();
int horizMargin = (size().width - width)/2;
int verMargin = (size().height - height)/2;
g.setColor(Color.white);
g.drawString(label, horizMargin,
verMargin + ascent + leading);
}
public boolean keyDown(Event evt, int key) {
TextField t =
(TextField)parent.h.get("keyDown");
t.setText(evt.toString());
return true;
}
public boolean keyUp(Event evt, int key) {
TextField t =
(TextField)parent.h.get("keyUp");
t.setText(evt.toString());
return true;
}
public boolean lostFocus(Event evt, Object w) {
TextField t =
(TextField)parent.h.get("lostFocus");
t.setText(evt.toString());
return true;
}
public boolean gotFocus(Event evt, Object w) {
TextField t =
(TextField)parent.h.get("gotFocus");
t.setText(evt.toString());
return true;
}
public boolean
mouseDown(Event evt,int x,int y) {
TextField t =
(TextField)parent.h.get("mouseDown");
t.setText(evt.toString());
return true;
}
public boolean
mouseDrag(Event evt,int x,int y) {
TextField t =
(TextField)parent.h.get("mouseDrag");
t.setText(evt.toString());
return true;
}
public boolean
mouseEnter(Event evt,int x,int y) {
TextField t =
(TextField)parent.h.get("mouseEnter");
t.setText(evt.toString());
return true;
}
public boolean
mouseExit(Event evt,int x,int y) {
TextField t =
(TextField)parent.h.get("mouseExit");
t.setText(evt.toString());
return true;
}
public boolean
mouseMove(Event evt,int x,int y) {
TextField t =
(TextField)parent.h.get("mouseMove");
t.setText(evt.toString());
return true;
}
public boolean mouseUp(Event evt,int x,int y) {
TextField t =
(TextField)parent.h.get("mouseUp");
t.setText(evt.toString());
return true;
}
}
public class AutoEvent extends Applet {
Hashtable h = new Hashtable();
String[] event = {
"keyDown", "keyUp", "lostFocus",
"gotFocus", "mouseDown", "mouseUp",
"mouseMove", "mouseDrag", "mouseEnter",
"mouseExit"
};
MyButton
b1 = new MyButton(this, Color.blue, "test1"),
b2 = new MyButton(this, Color.red, "test2");
public void init() {
setLayout(new GridLayout(event.length+1,2));
for(int i = 0; i < event.length; i++) {
TextField t = new TextField();
t.setEditable(false);
add(new Label(event[i], Label.CENTER));
add(t);
h.put(event[i], t);
}
add(b1);
add(b2);
}
} ///:~
//: Menu1.java
// Menus work only with Frames.
// Shows submenus, checkbox menu items
// and swapping menus.
import java.awt.*;
public class Menu1 extends Frame {
String[] flavors = { "Chocolate", "Strawberry",
"Vanilla Fudge Swirl", "Mint Chip",
"Mocha Almond Fudge", "Rum Raisin",
"Praline Cream", "Mud Pie" };
TextField t = new TextField("No flavor", 30);
MenuBar mb1 = new MenuBar();
Menu f = new Menu("File");
Menu m = new Menu("Flavors");
Menu s = new Menu("Safety");
// Alternative approach:
CheckboxMenuItem[] safety = {
new CheckboxMenuItem("Guard"),
new CheckboxMenuItem("Hide")
};
MenuItem[] file = {
new MenuItem("Open"),
new MenuItem("Exit")
};
// A second menu bar to swap to:
MenuBar mb2 = new MenuBar();
Menu fooBar = new Menu("fooBar");
MenuItem[] other = {
new MenuItem("Foo"),
new MenuItem("Bar"),
new MenuItem("Baz"),
};
Button b = new Button("Swap Menus");
public Menu1() {
for(int i = 0; i < flavors.length; i++) {
m.add(new MenuItem(flavors[i]));
// Add separators at intervals:
if((i+1) % 3 == 0)
m.addSeparator();
}
for(int i = 0; i < safety.length; i++)
s.add(safety[i]);
f.add(s);
for(int i = 0; i < file.length; i++)
f.add(file[i]);
mb1.add(f);
mb1.add(m);
setMenuBar(mb1);
t.setEditable(false);
add("Center", t);
// Set up the system for swapping menus:
add("North", b);
for(int i = 0; i < other.length; i++)
fooBar.add(other[i]);
mb2.add(fooBar);
}
public boolean handleEvent(Event evt) {
if(evt.id == Event.WINDOW_DESTROY)
System.exit(0);
else
return super.handleEvent(evt);
return true;
}
public boolean action(Event evt, Object arg) {
if(evt.target.equals(b)) {
MenuBar m = getMenuBar();
if(m == mb1) setMenuBar(mb2);
else if (m == mb2) setMenuBar(mb1);
}
else if(evt.target instanceof MenuItem) {
if(arg.equals("Open")) {
String s = t.getText();
boolean chosen = false;
for(int i = 0; i < flavors.length; i++)
if(s.equals(flavors[i])) chosen = true;
if(!chosen)
t.setText("Choose a flavor first!");
else
t.setText("Opening "+ s +". Mmm, mm!");
}
else if(evt.target.equals(file[1]))
System.exit(0);
// CheckboxMenuItems cannot use String
// matching; you must match the target:
else if(evt.target.equals(safety[0]))
t.setText("Guard the Ice Cream! " +
"Guarding is " + safety[0].getState());
else if(evt.target.equals(safety[1]))
t.setText("Hide the Ice Cream! " +
"Is it cold? " + safety[1].getState());
else
t.setText(arg.toString());
}
else
return super.action(evt, arg);
return true;
}
public static void main(String[] args) {
Menu1 f = new Menu1();
f.resize(300,200);
f.show();
}
} ///:~
//: ToeTest.java
// Demonstration of dialog boxes
// and creating your own components
import java.awt.*;
class ToeButton extends Canvas {
int state = ToeDialog.BLANK;
ToeDialog parent;
ToeButton(ToeDialog parent) {
this.parent = parent;
}
public void paint(Graphics g) {
int x1 = 0;
int y1 = 0;
int x2 = size().width - 1;
int y2 = size().height - 1;
g.drawRect(x1, y1, x2, y2);
x1 = x2/4;
y1 = y2/4;
int wide = x2/2;
int high = y2/2;
if(state == ToeDialog.XX) {
g.drawLine(x1, y1, x1 + wide, y1 + high);
g.drawLine(x1, y1 + high, x1 + wide, y1);
}
if(state == ToeDialog.OO) {
g.drawOval(x1, y1, x1+wide/2, y1+high/2);
}
}
public boolean
mouseDown(Event evt, int x, int y) {
if(state == ToeDialog.BLANK) {
state = parent.turn;
parent.turn= (parent.turn == ToeDialog.XX ?
ToeDialog.OO : ToeDialog.XX);
}
else
state = (state == ToeDialog.XX ?
ToeDialog.OO : ToeDialog.XX);
repaint();
return true;
}
}
class ToeDialog extends Dialog {
// w = number of cells wide
// h = number of cells high
static final int BLANK = 0;
static final int XX = 1;
static final int OO = 2;
int turn = XX; // Start with x's turn
public ToeDialog(Frame parent, int w, int h) {
super(parent, "The game itself", false);
setLayout(new GridLayout(w, h));
for(int i = 0; i < w * h; i++)
add(new ToeButton(this));
resize(w * 50, h * 50);
}
public boolean handleEvent(Event evt) {
if(evt.id == Event.WINDOW_DESTROY)
dispose();
else
return super.handleEvent(evt);
return true;
}
}
public class ToeTest extends Frame {
TextField rows = new TextField("3");
TextField cols = new TextField("3");
public ToeTest() {
setTitle("Toe Test");
Panel p = new Panel();
p.setLayout(new GridLayout(2,2));
p.add(new Label("Rows", Label.CENTER));
p.add(rows);
p.add(new Label("Columns", Label.CENTER));
p.add(cols);
add("North", p);
add("South", new Button("go"));
}
public boolean handleEvent(Event evt) {
if(evt.id == Event.WINDOW_DESTROY)
System.exit(0);
else
return super.handleEvent(evt);
return true;
}
public boolean action(Event evt, Object arg) {
if(arg.equals("go")) {
Dialog d = new ToeDialog(
this,
Integer.parseInt(rows.getText()),
Integer.parseInt(cols.getText()));
d.show();
}
else
return super.action(evt, arg);
return true;
}
public static void main(String[] args) {
Frame f = new ToeTest();
f.resize(200,100);
f.show();
}
} ///:~
//: FileDialogTest.java
// Demonstration of File dialog boxes
import java.awt.*;
public class FileDialogTest extends Frame {
TextField filename = new TextField();
TextField directory = new TextField();
Button open = new Button("Open");
Button save = new Button("Save");
public FileDialogTest() {
setTitle("File Dialog Test");
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.add(open);
p.add(save);
add("South", p);
directory.setEditable(false);
filename.setEditable(false);
p = new Panel();
p.setLayout(new GridLayout(2,1));
p.add(filename);
p.add(directory);
add("North", p);
}
public boolean handleEvent(Event evt) {
if(evt.id == Event.WINDOW_DESTROY)
System.exit(0);
else
return super.handleEvent(evt);
return true;
}
public boolean action(Event evt, Object arg) {
if(evt.target.equals(open)) {
// Two arguments, defaults to open file:
FileDialog d = new FileDialog(this,
"What file do you want to open?");
d.setFile("*.java"); // Filename filter
d.setDirectory("."); // Current directory
d.show();
String openFile;
if((openFile = d.getFile()) != null) {
filename.setText(openFile);
directory.setText(d.getDirectory());
} else {
filename.setText("You pressed cancel");
directory.setText("");
}
}
else if(evt.target.equals(save)) {
FileDialog d = new FileDialog(this,
"What file do you want to save?",
FileDialog.SAVE);
d.setFile("*.java");
d.setDirectory(".");
d.show();
String saveFile;
if((saveFile = d.getFile()) != null) {
filename.setText(saveFile);
directory.setText(d.getDirectory());
} else {
filename.setText("You pressed cancel");
directory.setText("");
}
}
else
return super.action(evt, arg);
return true;
}
public static void main(String[] args) {
Frame f = new FileDialogTest();
f.resize(250,110);
f.show();
}
} ///:~
//: Button2New.java
// Capturing button presses
import java.awt.*;
import java.awt.event.*; // Must add this
import java.applet.*;
public class Button2New extends Applet {
Button
b1 = new Button("Button 1"),
b2 = new Button("Button 2");
public void init() {
b1.addActionListener(new B1());
b2.addActionListener(new B2());
add(b1);
add(b2);
}
class B1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
getAppletContext().showStatus("Button 1");
}
}
class B2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
getAppletContext().showStatus("Button 2");
}
}
/* The old way:
public boolean action(Event evt, Object arg) {
if(evt.target.equals(b1))
getAppletContext().showStatus("Button 1");
else if(evt.target.equals(b2))
getAppletContext().showStatus("Button 2");
// Let the base class handle it:
else
return super.action(evt, arg);
return true; // We've handled it here
}
*/
} ///:~
|
Event, listener interface and add-
and remove-methods |
Components supporting this
event |
|---|---|
|
ActionEvent |
Button, List,
TextField, MenuItem, and its derivatives including CheckboxMenuItem,
Menu, and PopupMenu |
|
AdjustmentEvent |
Scrollbar |
|
ComponentEvent |
Component and its
derivatives, including Button, Canvas, Checkbox,
Choice, Container, Panel, Applet, ScrollPane,
Window, Dialog, FileDialog, Frame, Label,
List, Scrollbar, TextArea, and
TextField |
|
ContainerEvent |
Container and its
derivatives, including Panel, Applet, ScrollPane,
Window, Dialog, FileDialog, and
Frame |
|
FocusEvent |
Component and its
derivatives, including Button, Canvas, Checkbox,
Choice, Container, Panel, Applet, ScrollPane,
Window, Dialog, FileDialog, Frame Label,
List, Scrollbar, TextArea, and
TextField |
|
KeyEvent |
Component and its
derivatives, including Button, Canvas, Checkbox,
Choice, Container, Panel, Applet, ScrollPane,
Window, Dialog, FileDialog, Frame, Label,
List, Scrollbar, TextArea, and
TextField |
|
MouseEvent (for both clicks
and
motion) |
Component and its
derivatives, including Button, Canvas, Checkbox,
Choice, Container, Panel, Applet, ScrollPane,
Window, Dialog, FileDialog, Frame, Label,
List, Scrollbar, TextArea, and
TextField |
|
MouseEvent[55]
(for both clicks and
motion) |
Component and its
derivatives, including Button, Canvas, Checkbox,
Choice, Container, Panel, Applet, ScrollPane,
Window, Dialog, FileDialog, Frame, Label,
List, Scrollbar, TextArea, and
TextField |
|
WindowEvent |
Window and its derivatives,
including Dialog, FileDialog, and Frame |
|
ItemEvent |
Checkbox,
CheckboxMenuItem, Choice, List, and anything that
implements the ItemSelectable interface |
|
TextEvent |
Anything derived from
TextComponent, including TextArea and
TextField |
|
Component type |
Events supported by this
component |
|---|---|
|
Adjustable |
AdjustmentEvent |
|
Applet |
ContainerEvent, FocusEvent,
KeyEvent, MouseEvent, ComponentEvent |
|
Button |
ActionEvent, FocusEvent,
KeyEvent, MouseEvent, ComponentEvent |
|
Canvas |
FocusEvent, KeyEvent,
MouseEvent, ComponentEvent |
|
Checkbox |
ItemEvent, FocusEvent, KeyEvent,
MouseEvent, ComponentEvent |
|
CheckboxMenuItem |
ActionEvent,
ItemEvent |
|
Choice |
ItemEvent, FocusEvent, KeyEvent,
MouseEvent, ComponentEvent |
|
Component |
FocusEvent, KeyEvent,
MouseEvent, ComponentEvent |
|
Container |
ContainerEvent, FocusEvent,
KeyEvent, MouseEvent, ComponentEvent |
|
Dialog |
ContainerEvent, WindowEvent,
FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
|
FileDialog |
ContainerEvent, WindowEvent,
FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
|
Frame |
ContainerEvent, WindowEvent,
FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
|
Label |
FocusEvent, KeyEvent,
MouseEvent, ComponentEvent |
|
List |
ActionEvent, FocusEvent,
KeyEvent, MouseEvent, ItemEvent, ComponentEvent |
|
Menu |
ActionEvent |
|
MenuItem |
ActionEvent |
|
Panel |
ContainerEvent, FocusEvent,
KeyEvent, MouseEvent, ComponentEvent |
|
PopupMenu |
ActionEvent |
|
Scrollbar |
AdjustmentEvent, FocusEvent,
KeyEvent, MouseEvent, ComponentEvent |
|
ScrollPane |
ContainerEvent, FocusEvent,
KeyEvent, MouseEvent, ComponentEvent |
|
TextArea |
TextEvent, FocusEvent, KeyEvent,
MouseEvent, ComponentEvent |
|
TextComponent |
TextEvent, FocusEvent, KeyEvent,
MouseEvent, ComponentEvent |
|
TextField |
ActionEvent, TextEvent,
FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
|
Window |
ContainerEvent, WindowEvent,
FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
|
Listener
interface |
Methods in
interface |
|---|---|
|
ActionListener |
actionPerformed(ActionEvent) |
|
AdjustmentListener |
adjustmentValueChanged( |
|
ComponentListener |
componentHidden(ComponentEvent) |
|
ContainerListener |
componentAdded(ContainerEvent) |
|
FocusListener |
focusGained(FocusEvent) |
|
KeyListener |
keyPressed(KeyEvent) |
|
MouseListener |
mouseClicked(MouseEvent) |
|
MouseMotionListener |
mouseDragged(MouseEvent) |
|
WindowListener |
windowOpened(WindowEvent) |
|
ItemListener |
itemStateChanged(ItemEvent) |
|
TextListener |
textValueChanged(TextEvent) |
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
class MyWindowListener extends WindowAdapter {
public void WindowClosing(WindowEvent e) {
System.exit(0);
}
}
//: Button2NewB.java
// An application and an applet
import java.awt.*;
import java.awt.event.*; // Must add this
import java.applet.*;
public class Button2NewB extends Applet {
Button
b1 = new Button("Button 1"),
b2 = new Button("Button 2");
TextField t = new TextField(20);
public void init() {
b1.addActionListener(new B1());
b2.addActionListener(new B2());
add(b1);
add(b2);
add(t);
}
class B1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
t.setText("Button 1");
}
}
class B2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
t.setText("Button 2");
}
}
// To close the application:
static class WL extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
// A main() for the application:
public static void main(String[] args) {
Button2NewB applet = new Button2NewB();
Frame aFrame = new Frame("Button2NewB");
aFrame.addWindowListener(new WL());
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(300,200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
} ///:~
aFrame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});