<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init()">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
internal function init():void
{
//设置数组的内容
var arr:ArrayCollection = new ArrayCollection([{label:"+", value:"+"},
{label:"-", value:"-"}, {label:"*", value:"*"},
{label:"/", value:"/"}]) ;
sel.dataProvider = arr; //将下拉框与数组的值绑定
}
internal function do_btn():void
{
var a:Number=(Number)(num1.text);
var b:Number=(Number)(num2.text);
var caculate:String= sel.selectedItem.value;;
var result:Number;
if(isNaN(a)==false || isNaN(b)==false)
{
result=cac(a,b,caculate);
res.text=result.toString();
}
else
Alert.show("请输入数字");
}
internal function cac(a:Number, b:Number,caculate:String) //定义cac函数,用于计算两个数的结果
{
if(caculate=="+") //如果为加法则相加
return a+b;
if(caculate=="-") //如果为减法则相减
return a-b;
if(caculate=="*") //如果为乘法则返回乘积
return a*b;
if(caculate=="/")
{
if(b==0) //判断除数是否为0
Alert.show("除数不能等于0");
else
return a/b; //除数不为0则相除
}
}
]]>
</fx:Script>
<s:Panel x="175" y="59" width="275" height="228" title="计算器">
<s:TextInput x="23" y="53" width="59" id="num1"/>
<s:DropDownList x="88" y="53" width="51" id="sel"></s:DropDownList>
<s:TextInput x="147" y="53" width="59" id="num2"/>
<s:Button x="213" y="54" label="计算" width="50" click="do_btn()"/>
<s:Label x="35" y="104" text="结果:" fontSize="14"/>
<s:TextInput x="85" y="104" id="res"/>
</s:Panel>
</s:Application>