<?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>
flex 制作的简单计算器
最新推荐文章于 2023-09-22 19:32:46 发布