加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex4学习笔记(一)--基础控件的使用

发布时间:2020-12-15 00:58:27 所属栏目:百科 来源:网络整理
导读:1.一个最简单的例子,按钮点击事件 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 fx:Script ???? ![CDATA[ ???????? import mx.controls.Alert; protected function btn_clickHandler(event:MouseEvent): void ???????? { ???????????? var btn:Button = event

1.一个最简单的例子,按钮点击事件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<fx:Script>
???? <![CDATA[
???????? import mx.controls.Alert;
protected function btn_clickHandler(event:MouseEvent): void
???????? {
???????????? var btn:Button = event.target as Button;
???????????? Alert.show(btn.id);
}
]]>
</fx:Script>
?
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Button x= "132" y= "179" label = "按钮1" id= "btn1" click= "btn_clickHandler(event)" />
"280" "按钮2" "btn2" />

?

这个例子我添加了两个按钮,共用了同一个事件,在事件里可以通过event.target来判断到底点击的是哪一个按钮。

?

2.加载图片的例子,并且允许手工设置图片的宽和高

16
17
18
19
?
button1_clickHandler(event:MouseEvent): img.width = Number (imgWidth.text);
img.height = (imgHeight.text);
?
]]>
</fx:Script>
?
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:Image x= "106" "162" source= "@Embed('/test/1.jpg')" width= "211" height= "182" "img" />
"220" "38" "设置" "button1_clickHandler(event)" />
<s:TextInput x= "109" "38" "35" "imgWidth" />
"imgHeight" />

?

我把1.jpg放到test包里了,写路径的话就需要这么写:@Embed('/test/1.jpg'),不然可以会出现“无法解析用于转换代码”的错误;

Number()用于将给定值转换成数字值,用法很简单;

如果需要让图片加载的时候宽度和高度跟图片原始宽高度一样,可以这么写:<mx:Image x="106" y="162" source="@Embed('/test/1.jpg')" width="100%" height="100%" id="img" autoLoad="true" scaleContent="false"/>

?

3.加载swf的例子

?
1
<mx:SWFLoader x= "5" "@Embed('a.swf')" scaleContent= "false" autoLoad= "true" "600" "500" />

?

?

4.调色板的使用

14
mx.events.ColorPickerEvent;
?
colorpicker1_changeHandler(event:ColorPickerEvent): void
{
text1.setStyle( "color" ,event.color);
<s:TextInput id= "text1" x= "114" "120" color= "#FF0000" />
<mx:ColorPicker x= "250" change= "colorpicker1_changeHandler(event)" />

该例子有一个输入框,输入框内文字默认是红色,可以通过调色板修改输入框内字体颜色

?

5.下拉框DropDownList和ComboBox的使用

19
20
mx.collections.ArrayCollection;
?????????
[Bindable]
private var dp:ArrayCollection = new ArrayCollection([{id: 1 '苹果' },{id: 2 '梨' 3 '香蕉' }]);
?????????
func(item: Object ): String
{
???????????? return "水果:" + item.name;
}
]]>
</fx:Script>
?
<s:DropDownList x= "157" "89" dataProvider= "{dp}" labelField= "name" selectedIndex= "0" "159" labelFunction= "func" ?
<s:ComboBox x= "291" />
/>

?

使用DropDownList控件,下拉框是只读的,使用ComboBox控件的话,下拉框的内容可以输入;

使用[Bindable],表示定义的数据可以做为数据源引用,在Flex4中不添加该内容也没有问题,只是代码会有警告提示,建议使用

?

6.使用控件MenuBar添加一个菜单,并且在子菜单点击时执行事件

20
21
22
23
24
25
26
27
28
29
mx.events.MenuEvent;
?
menubar1_itemClickHandler(event:MenuEvent): void
{
Alert.show(event.item.@ label );
}
?
]]>
</fx:Script>
?
<mx:MenuBar x= "118" "124" "@label" itemClick= "menubar1_itemClickHandler(event)" >
<mx:dataProvider>
<mx:XMLListCollection>
<fx:XMLList xmlns= "" >
???????????????? <menu "aa" >
???????????????????? <item "aa1" />
"aa2" />
</menu>
"bb" >
"bb1" />
"bb2" />
</menu>??????????????????
</fx:XMLList>
</mx:XMLListCollection>
</mx:dataProvider>
</mx:MenuBar>

?

7.日期控件DateField以及DateChooser的简单用法

15
mx.events.CalendarLayoutChangeEvent;
datechooser1_changeHandler(event:CalendarLayoutChangeEvent): text1.text = event.newDate.getFullYear().toString() + "-" + (event.newDate.getMonth()+ ).toString() + + event.newDate.getDate().toString();
<mx:DateField x= "110" "128" formatString= "YYYY-MM-DD" />
<mx:DateChooser x= "374" "datechooser1_changeHandler(event)" />
"98" />

1.一个最简单的例子,按钮点击事件

"211"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

相关内容
推荐文章
站长推荐
热点阅读