同学前面三个比较简单..最后一个要建个mxml.可以自己尝试一下,不懂了可以M我
1:
var student:Object = new Object;
student.name = "zhangsan";
student.age = 20;
student.sex = "男";
2:
(1):sex:男;
age:20;
name:zhangsan;
(2):男
20
zhangsan
3:以前写的一个计算器 给你参考一下;
计算器逻辑代码:
package com.calculator.ui
{
import com.calculator.event.ButtonChangeEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import mx.containers.VBox;
import mx.controls.Button;
import mx.controls.TextInput;
import mx.events.FlexEvent;
public class CalculatorMainBox extends VBox
{
private var txtIn:TextInput;
private var btn:Button;
public function CalculatorMainBox()
{
addEventListener(FlexEvent.INITIALIZE,init);
setStyle("verticalGap",5);
setStyle("top",10);
addEventListener(ButtonChangeEvent.GET_BUTTON_LABEL,getButtonValue);
addEventListener(ButtonChangeEvent.GET_BUTTON_EQUAL,getTextValue)
}
private function init(event:FlexEvent):void
{
paint();
}
private function getTextValue(event:ButtonChangeEvent):void
{
calculate();
}
private var valueStr:String;
private var value:String;
private var initialValue:String="0";
[Bindable]private var initiaLValue_1:String;
private var lastValue:String;
private function getButtonValue(event:ButtonChangeEvent):void
{
if(isNaN(parseFloat(initialValue.charAt(initialValue.length-1))))
{
initiaLValue_1 = txtIn.text;
txtIn.text = "0";
initialValue = "0";
}
if(!(isNaN(parseFloat(event.getButtonVaule))))
{
if(txtIn.text=="0")
txtIn.text = "";
txtIn.text+=event.getButtonVaule;
}
else
{
if(isNaN(parseFloat(txtIn.text.charAt(txtIn.text.length))))
{
if(txtIn.text.indexOf(".")==-1)
{
if(event.getButtonVaule =="."||!(isNaN(parseFloat(event.getButtonVaule))))
{
if(txtIn.text.charAt(txtIn.text.length)!=".")
txtIn.text+=".";
}
}
}
if(event.getButtonVaule == "+/-")
{
if(parseFloat(txtIn.text)>0)
{
txtIn.text = "-"+txtIn.text;
}
else if(parseFloat(txtIn.text)<0)
{
txtIn.text = txtIn.text.substr(1,txtIn.text.length-1);
}
}
if(event.getButtonVaule=="+"||event.getButtonVaule=="-"||event.getButtonVaule=="*"||event.getButtonVaule=="/")
{
value = event.getButtonVaule;
getInitialValue(event.getButtonVaule);
}
}
valueStr+=event.getButtonVaule;
}
private function clean(event:MouseEvent):void
{
txtIn.text = "0";
initialValue = "0";
initiaLValue_1 = "0";
}
[Bindable]private var result:Number;
private function getInitialValue(str:String):void
{
initialValue = txtIn.text+str;
//calculate();
}
private function calculate():void
{
if(value=="+")
txtIn.text = (parseFloat(initiaLValue_1)+parseFloat(txtIn.text))+"";
if(value=="-")
txtIn.text = (parseFloat(initiaLValue_1)-parseFloat(txtIn.text))+"";
if(value=="*")
txtIn.text = (parseFloat(initiaLValue_1)*parseFloat(txtIn.text))+"";
if(value=="/")
txtIn.text = (parseFloat(initiaLValue_1)/parseFloat(txtIn.text))+"";
}
private function paint():void
{
txtIn = new TextInput();
txtIn.width = 235;
txtIn.text = "0";
txtIn.setStyle("textAlign","right");
txtIn.setStyle("FontWeight","NORMAL");
txtIn.restrict = "0-9";
addChild(txtIn);
btn = new Button;
btn.label = "C";
btn.setStyle("color","red");
btn.width = 235;
btn.addEventListener(MouseEvent.CLICK,clean);
addChild(btn);
var key:CalculatorKey = new CalculatorKey;
addChild(key);
}
}
}
计算器页面布局代码:
package com.calculator.ui
{
import com.calculator.event.ButtonChangeEvent;
import flash.events.MouseEvent;
import mx.containers.HBox;
import mx.containers.Tile;
import mx.controls.Button;
import mx.events.FlexEvent;
public class CalculatorKey extends HBox
{
private var butTile:Tile;
private var keyArr:Array = new Array(7,8,9,"/",4,5,6,"*",1,2,3,"-",0,"+/-",".","+");
public function CalculatorKey()
{
addEventListener(FlexEvent.INITIALIZE,init);
setStyle("horizontalGap",2);
}
private function init(event:FlexEvent):void
{
paint();
}
private function getLabel(event:MouseEvent):void
{
var evt:ButtonChangeEvent = new ButtonChangeEvent(ButtonChangeEvent.GET_BUTTON_LABEL);
evt.getButtonVaule = (event.target as Button).label;
this.dispatchEvent(evt);
}
private function paint():void
{
butTile = new Tile;
butTile.setStyle("horizontalGap",1);
butTile.setStyle("verticalGap",8);
for(var i:int=0;i { var btn:Button = new Button; btn.label = keyArr[i].toString(); if(keyArr[i] is Number) { btn.setStyle("color","blue"); } else { btn.setStyle("color","red"); } btn.addEventListener(MouseEvent.CLICK,getLabel); butTile.addChild(btn); } addChild(butTile); var btn_1:Button = new Button; btn_1.addEventListener(MouseEvent.CLICK,getMyEQ); btn_1.setStyle("color","red"); btn_1.label = "="; btn_1.height = 113; addChild(btn_1); } private function getMyEQ(event:MouseEvent):void { this.dispatchEvent(new ButtonChangeEvent(ButtonChangeEvent.GET_BUTTON_EQUAL)); } } } 计算器事件代码: package com.calculator.event { import flash.events.Event; public class ButtonChangeEvent extends Event { public static const GET_BUTTON_LABEL:String = "get_button_label"; public static const GET_BUTTON_EQUAL:String ="get_button_equal"; public var getButtonVaule:String; public function ButtonChangeEvent(type:String,bubbles:Boolean = true,user_capture:Boolean = false) { super(type,bubbles,user_capture); } } } 计算器外部的panel容器代码: package com.calculator.ui { import mx.containers.Panel; public class CalculatorPanel extends Panel { public function CalculatorPanel() { super(); this.title = "计算器"; setStyle("paddingLeft",10); setStyle("paddingRight",10); setStyle("paddingTop",10); setStyle("paddingBottom",10); } } } 4:
import mx.controls.Alert; protected function button1_clickHandler(event:MouseEvent):void { if(user_name.text=="张三"&&user_pass.text=="12345") { Alert.show("欢迎登录","提示:"); }else{ clean(); } } private function clean():void { user_name.text = ""; user_pass.text = ""; } ]]> 计算器图片
ere45r45r45454rt