<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal"> <mx:Panel width="237" height="252" layout="absolute" title="计算器" horizontalAlign="center" verticalAlign="middle" backgroundColor="#AFB6B9" horizontalCenter="0"> <mx:TextInput x="9" y="10" width="215" id="txtResult" textAlign="right" fontSize="20" enabled="true" height="38" text="0" fontWeight="bold"/> <mx:Button x="12" y="135" label="1" width="36" height="31" id="bt_1" click="btnClick('1')"/> <mx:Button x="56" y="135" label="2" width="36" height="31" id="bt_2" click="btnClick('2')"/> <mx:Button x="100" y="135" label="3" width="36" height="31" id="bt_3" click="btnClick('3')"/> <mx:Button x="12" y="96" label="4" width="36" height="31" id="bt_4" click="btnClick('4')"/> <mx:Button x="56" y="96" label="5" width="36" height="31" id="bt_5" click="btnClick('5')"/> <mx:Button x="100" y="96" label="6" width="36" height="31" id="bt_6" click="btnClick('6')"/> <mx:Button x="12" y="57" label="7" width="36" height="31" color="#0B0C0F" id="bt_7" click="btnClick('7')"/> <mx:Button x="56" y="57" label="8" width="36" height="31" id="bt_8" click="btnClick('8')"/> <mx:Button x="100" y="57" label="9" width="36" height="31" id="bt_9" click="btnClick('9')"/> <mx:Button x="56" y="174" label="0" width="36" height="31" id="bt_0" click="btnClick('0')"/> <mx:Button x="8" y="174" label="." width="36" height="31" id="bt_dot" click="btnClick('.')"/> <mx:Button x="100" y="174" label="=" width="36" height="31" id="bt_equal" click="getResult()"/> <mx:Button x="143.75" y="56" label="CE" width="81" height="71" id="bt_ce" click="txtResult.text=''"/> <mx:Button x="144" y="135" label="+" width="36" height="31" color="#000000" id="bt_plus" click="btnCalc('+')"/> <mx:Button x="144" y="174" label="-" width="36" height="31" color="#000000" id="bt_minus" click="btnCalc('-')"/> <mx:Button x="188" y="135" label="*" width="36" height="31" color="#000000" id="bt_multiply" click="btnCalc('*')"/> <mx:Button x="188" y="174" label="/" width="36" height="31" color="#000000" id="bt_divide" click="btnCalc('/')"/> </mx:Panel> <mx:Script> <![CDATA[ import mx.controls.Alert; private var r1:Number;//第一个数 private var r2:Number;//第二个数 private var opear:String;//操作符 /* 判断点击数*/ private function btnClick(n:String):void{ var r:String=txtResult.text;//记录当前文本框内容 if(r.length==1&&r=="0"){//如果第一个数是0,清空结果 txtResult.text=""; txtResult.text=n.toString(); return; } if(r.length==1&&r=="."){ txtResult.text="0."+n.toString(); return; } if(r=="+"||r=="-"||r=="*"||r=="/"){//如果文本框是操作符,清空结果 txtResult.text=""; txtResult.text=n.toString(); }else{ r==""?txtResult.text=n:txtResult.text=(r+n);//三元表达式 } } /* 判断操作符 */ private function btnCalc(op:String):void{ r1=Number(txtResult.text);//第一个数 txtResult.text=""; opear=op; switch(opear){ case "+": txtResult.text="+"; break; case "-": txtResult.text="-"; break; case "*": txtResult.text="*"; break; case "/": txtResult.text="/"; break; } } /* 计算结果 */ private function getResult():void{ r2=Number(txtResult.text); switch(opear){ case "+": txtResult.text=(r1+r2).toString(); break; case "-": txtResult.text=(r1-r2).toString(); break; case "*": txtResult.text=(r1*r2).toString(); break; case "/": txtResult.text=(r1/r2).toString(); break; } } ]]> </mx:Script> </mx:Application>