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

flex 学习笔记 标签与as里面的类(一)

发布时间:2020-12-15 04:58:41 所属栏目:百科 来源:网络整理
导读:Flex最强大的特性之一就是它在标签和ActionScript类之间创建了一个简单的映射。 就像下面的样子: ?1 —— 每个标签相当于一个以标签名为类名的类的一个实例。 ?2 —— 标签的每个属性(attribute)转变为对象中的一个property。 ?3 —— 每个标签中的id属性

Flex最强大的特性之一就是它在标签和ActionScript类之间创建了一个简单的映射。

就像下面的样子:
?1 —— 每个标签相当于一个以标签名为类名的类的一个实例。
?2—— 标签的每个属性(attribute)转变为对象中的一个property。
?3—— 每个标签中的id属性转变为相应实例中的一个变量。

例如? 类:

 
 
  1. public?class?Contact ?
  2. { ?
  3. ????public?var?home?:?Location; ?
  4. ????public?var?work?:?Location; ?
  5. ????public?var?firstname?:?String; ?
  6. ????public?var?lastname?:?String; ?
  7. ????public?var?isFriend?:?Boolean?=?false; ?
  8. } ?

标签:<Contact id="myContact" firstname="Susan" lastname="Smith" isfriend="true" />

相当于:

 
 
  1. var?myContact?:?Contact?=?new?Contact(); ?
  2. myContact.firstname?=?"Susan"; ?
  3. myContact.lastname="Smith"; ?
  4. myContact.isFriend=true; ?

所以就可以这样:

代码:

 
 
  1. <?xml?version="1.0"?encoding="utf-8"?>?
  2. <contr:Test05?xmlns:fx="http://ns.adobe.com/mxml/2009"? ?
  3. ??????????????xmlns:s="library://ns.adobe.com/flex/spark"? ?
  4. ??????????????xmlns:mx="library://ns.adobe.com/flex/mx"?xmlns:contr="contr.*"?width="400"?height="300">?
  5. ????<fx:Declarations>?
  6. ????????<!--?将非可视元素(例如服务、值对象)放在此处?-->?
  7. ????</fx:Declarations>?
  8. ????<mx:VBox?width="100"?height="100">?
  9. ????????<s:Label?width="30"?height="30"?text="12312"?fontSize="40"/>?
  10. ????</mx:VBox>?
  11. </contr:Test05>?

?

 
 
  1. package?contr ?
  2. { ?
  3. ????import?flash.events.MouseEvent; ?
  4. ????import?mx.containers.TitleWindow; ?
  5. ????import?mx.controls.Button; ?
  6. ????import?mx.controls.LinkButton; ?
  7. ????import?mx.managers.PopUpManager; ?
  8. ????public?class?Test05?extends?TitleWindow ?
  9. ????{ ?
  10. ????????private?var?helpButton?:?LinkButton; ?
  11. ????????private?var?_closeButton?:?Button; ?
  12. ????????public?function?Test05?() ?
  13. ????????{ ?
  14. ????????????title?=?"Custom?TitleWindow"; ?
  15. ????????????showCloseButton?=?true; ?
  16. ????????} ?
  17. ????????private?function?get?closeButton?()?:?Button ?
  18. ????????{ ?
  19. ????????????if?(!?_closeButton) ?
  20. ????????????{ ?
  21. ????????????????for?(var?i?:?int?=?0;?i?<?titleBar.numChildren;?++?i) ?
  22. ????????????????{ ?
  23. ????????????????????if?(titleBar.getChildAt?(i)?is?Button?&& ?
  24. ????????????????????????titleBar.getChildAt?(i)?!=?helpButton) ?
  25. ????????????????????{ ?
  26. ????????????????????????_closeButton?=?titleBar.getChildAt?(i)?as ?
  27. ????????????????????????????Button; ?
  28. ????????????????????} ?
  29. ????????????????} ?
  30. ????????????} ?
  31. ????????????return?_closeButton; ?
  32. ????????} ?
  33. ????????override?protected?function?createChildren?()?:?void ?
  34. ????????{ ?
  35. ????????????super.createChildren?();???????????????? ?
  36. ????????????if?(!?helpButton) ?
  37. ????????????{ ?
  38. ????????????????helpButton?=?new?LinkButton?(); ?
  39. ????????????????helpButton.label?=?"Help";?????????? ?
  40. ????????????????helpButton.focusEnabled?=?false; ?
  41. ????????????????helpButton.setStyle?("paddingTop",?4); ?
  42. ????????????????titleBar.addChild?(helpButton); ?
  43. ????????????????helpButton.addEventListener(MouseEvent.CLICK,helpClick) ?
  44. ????????????????helpButton.owner?=?this; ?
  45. ????????????} ?
  46. ????????} ?
  47. ????????protected?function?helpClick(event:MouseEvent):void ?
  48. ????????{ ?
  49. ????????????PopUpManager.addPopUp(this,this,true); ?
  50. ????????} ?
  51. ????????override?protected?function?layoutChrome?(w?:?Number,?
  52. ??????????????????????????????????????????????????h?:?Number)?:?void ?
  53. ????????{ ?
  54. ????????????super.layoutChrome?(w,?h); ?
  55. ????????????var?width?:?Number?= ?
  56. ????????????????helpButton.getExplicitOrMeasuredWidth?(); ?
  57. ????????????var?height?:?Number?= ?
  58. ????????????????helpButton.getExplicitOrMeasuredHeight?(); ?
  59. ????????????var?x?:?Number?=?5; ?
  60. ????????????var?y?:?Number?=?5; ?
  61. ????????????helpButton.setActualSize?(width,?height); ?
  62. ????????????helpButton.move?(x,?y); ?
  63. ????????} ?
  64. ????} ?
  65. }??

在类里面可以使用任何这个类的属性和方法来定义要使用的组件的样式和事件逻辑。

学习ING。。。。

(编辑:李大同)

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

    推荐文章
      热点阅读