<? xml version="1.0" encoding="utf-8" ?> <!-- 在mx:Application标签中,输入随后的属性值以便createListener()功能函数被调用,并且使事件监听器在程序被创建后立即被注册: --> <!-- 每当用户点击btnConvert 按钮,convertCurrency 事件监听器就被通知相应的触发事件已经发生。监听器函数执行货币计算并显示结果。 --> <!-- 每当监听器的功能函数被调用时,Flex 创建一个事件对象并传递给监听器。因此,最好的办法是在监听器的功能函数中声明事件对象。因此,你在convertCurrency 函数中声明了一个叫e 的事件类型对象。 --> < mx:Application xmlns:mx ="http://www.adobe.com/2006/mxml" layout ="absolute" creationComplete ="createListener();" > < mx:Script > <![CDATA[ public function createListener():void { cmdConvert.addEventListener(MouseEvent.CLICK, convertCurrency); } public function convertCurrency(e:Event):void { var rate:Number = 120; var price:Number = Number(txtPrice.text); if (isNaN(price)) { lblResults.text = "Please enter a valid price."; } else { price = price * rate; lblResults.text = "Price in Yen: " + String(price); } } ]]> </ mx:Script > < mx:Panel x ="10" y ="10" width ="410" height ="200" layout ="absolute" > < mx:Label x ="10" y ="10" text ="请输入美元" width ="76" /> < mx:Text y ="10" id ="txtPrice" height ="20" text ="10" enabled ="true" width ="149" x ="81" /> < mx:Button x ="252" y ="8" id ="cmdConvert" label ="转换" /> < mx:Label x ="10" y ="59" id ="lblResults" width ="228" text ="轮换后" /> </ mx:Panel > </ mx:Application >