flex 制作的简单计算器

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal">
    <mx:Panel width="382" height="277" layout="absolute">
        <mx:Button x="48" y="37" label="1" width="38" height="23" id="btn1" click="btnClick('1')"/>
        <mx:Button x="223" y="81" label="8" width="38" height="23" id="btn8" click="btnClick('8')"/>
        <mx:Button x="168" y="81" label="7" width="38" height="23" id="btn7" click="btnClick('7')"/>
        <mx:Button x="108" y="81" label="6" width="38" height="23" id="btn6" click="btnClick('6')"/>
        <mx:Button x="48" y="81" label="5" width="38" height="23" id="btn5" click="btnClick('5')"/>
        <mx:Button x="108" y="129" label="0" width="38" height="23" id="btn0" click="btnClick('0')"/>
        <mx:Button x="48" y="129" label="9" width="38" height="23" id="btn9" click="btnClick('9')"/>
        <mx:Button x="289" y="42" label="=" width="48" height="151" color="#0B0BF5" fontWeight="bold" id="btnResult" click="getResult()"/>
        <mx:Button x="223" y="37" label="4" width="38" height="23" id="btn4" click="btnClick('4')"/>
        <mx:Button x="168" y="37" label="3" width="38" height="23" id="btn3" click="btnClick('3')"/>
        <mx:Button x="108" y="37" label="2" width="38" height="23" id="btn2" click="btnClick('2')"/>
        <mx:Button x="168" y="129" label="." width="38" height="23" id="btnDian" click="btnClick('.')"/>
        <mx:Button x="223" y="129" label="C" width="38" height="23" color="#0B0BF5" fontWeight="bold" id="btnClear" click="txtResult.text=''"/>
        <mx:Button x="223" y="170" label="/" width="38" height="23" color="#FC0101" fontWeight="bold" id="btnChu" click="btnCalc('/')"/>
        <mx:Button x="168" y="170" label="*" width="38" height="23" color="#FC0101" fontWeight="bold" id="btnChen" click="btnCalc('*')"/>
        <mx:Button x="108" y="170" label="-" width="38" height="23" color="#FC0101" fontWeight="bold" id="btnJian" click="btnCalc('-')"/>
        <mx:Button x="48" y="170" label="+" width="38" height="23" color="#FC0101" fontWeight="bold" id="btnAdd" click="btnCalc('+')"/>
        <mx:TextInput x="23" y="10" width="329" id="txtResult" textAlign="right" fontSize="12" enabled="true"/>
    </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=="+"||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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值