【环境】
开发包:flex sdk4.6.0
开发工具:FlashBuilder4.6
运行环境:AdobeAir程序
【需求】
写一个计算器,满足加减乘除的基本需求,界面上的“S”按钮表示求平方根,”G“表示求平方,”N“表示取倒数。
【代码】
<?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" creationComplete="windowedapplication1_creationCompleteHandler()" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"> <s:layout> <s:VerticalLayout horizontalAlign="center" verticalAlign="middle" /> </s:layout> <fx:Script> <![CDATA[ import mx.controls.Alert; private var num1:Number; // 前一个数的值 private var hasOver:Boolean = false; // 是否按了计算符号 private var operator:String; // 运算符 protected function windowedapplication1_creationCompleteHandler():void { hasOver = false; result.text="0"; operator = ""; } private function showNumber(str:String):void { if(str == '.' && result.text.indexOf(".") != -1) return; if(result.text.length >= 9) return; if(result.text == "0" && str == "0") return; if(result.text == "NaN" || result.text == "0") { result.text = ""; } result.text += str; hasOver = false; } private function saveNumber(oper:String):void { if(hasOver && oper == "-") { result.text = "-"; return; } num1 = Number(result.text); result.text = "0"; hasOver = true; operator = oper; } private function calculate():void { switch(operator) { case "+": result.text = String(num1 + Number(result.text)); break; case "-": result.text = String(num1 - Number(result.text)); break; case "*": result.text = String(num1 * Number(result.text)); break; case "/": result.text = result.text == "0" ? "NaN" : String(num1 / Number(result.text)); break; default: break; } } private function simpleCal(type:String):void { switch(type) { case "sqrt": result.text = String(Math.sqrt(Number(result.text))); break; case "square": result.text = String(Math.pow(Number(result.text), 2)); break; case "divided": result.text = result.text == "0" ? "NaN" : String(1 / Number(result.text)); break; default: break; } } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <!--布局:Panel--> <s:Panel width="220" height="260" title="计算器"> <s:layout> <s:VerticalLayout horizontalAlign="center" verticalAlign="middle" /> </s:layout> <s:TextInput id="result" width="80%" height="25"/> <s:HGroup width="100%" height="30" paddingLeft="5" paddingRight="5" verticalAlign="middle" horizontalAlign="center"> <!-- paddingLeft左边距 paddingRight右边距 --> <s:Button width="40" label="S" click="simpleCal('sqrt')"/> <!--求平方根--> <s:Button width="40" label="G" click="simpleCal('square')"/> <!--求平方--> <s:Button width="40" label="N" click="simpleCal('divided')"/> <!--求1/N--> <s:Button width="40" label="C" click="windowedapplication1_creationCompleteHandler()"/> </s:HGroup> <s:HGroup width="100%" height="30" paddingLeft="5" paddingRight="5" verticalAlign="middle" horizontalAlign="center"> <!-- paddingLeft左边距 paddingRight右边距 --> <s:Button width="40" label="1" click="showNumber('1')"/> <s:Button width="40" label="2" click="showNumber('2')"/> <s:Button width="40" label="3" click="showNumber('3')"/> <s:Button width="40" label="=" click="calculate()"/> </s:HGroup> <s:HGroup width="100%" height="30" paddingLeft="5" paddingRight="5" verticalAlign="middle" horizontalAlign="center"> <!-- paddingLeft左边距 paddingRight右边距 --> <s:Button width="40" label="4" click="showNumber('4')"/> <s:Button width="40" label="5" click="showNumber('5')"/> <s:Button width="40" label="6" click="showNumber('6')"/> <s:Button width="40" label="+" click="saveNumber('+')"/> </s:HGroup> <s:HGroup width="100%" height="30" paddingLeft="5" paddingRight="5" verticalAlign="middle" horizontalAlign="center"> <!-- paddingLeft左边距 paddingRight右边距 --> <s:Button width="40" label="7" click="showNumber('7')"/> <s:Button width="40" label="8" click="showNumber('8')"/> <s:Button width="40" label="9" click="showNumber('9')"/> <s:Button width="40" label="-" click="saveNumber('-')"/> </s:HGroup> <s:HGroup width="100%" height="30" paddingLeft="5" paddingRight="5" verticalAlign="middle" horizontalAlign="center"> <!-- paddingLeft左边距 paddingRight右边距 --> <s:Button width="40" label="0" click="showNumber('0')"/> <s:Button width="40" label="." click="showNumber('.')"/> <s:Button width="40" label="/" click="saveNumber('/')"/> <s:Button width="40" label="*" click="saveNumber('*')"/> </s:HGroup> </s:Panel> </s:WindowedApplication>