|
|
楼主 |
发表于 2009-4-27 04:11:58
|
显示全部楼层
以上是使用 MXML
下面的示例阐述如何通过使用 ActionScript 创建 Button 控件。 该结果与该 MXML 版本是相同的。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
viewSourceURL="src/GettingStartedActionScript/index.html"
creationComplete="creationCompleteHandler();"
width="300" height="80"
>
<mx:Script>
<![CDATA[
import mx.controls.Button;
import mx.events.FlexEvent;
private var myButton:Button;
private function creationCompleteHandler():void
{
// Create a Button instance and set its label
myButton = new Button();
myButton.label = "I'm a button!";
// Get notified once button component has been created and processed for layout
myButton.addEventListener (FlexEvent.CREATION_COMPLETE, buttonCreationCompleteHandler);
// Add the Button instance to the DisplayList
addChild (myButton);
}
private function buttonCreationCompleteHandler ( evt:FlexEvent ):void
{
// Center the button
myButton.x = parent.width/2 - myButton.width/2;
myButton.y = parent.height/2 - myButton.height/2;
}
]]>
</mx:Script>
</mx:Application>
通过 ActionScript 创建 Flex 组件时, 必须导入组件的类。 您还必须通过使用 addChild() 方法使组件可见, 将组件添加到应用程序的 DisplayList 中。 通过将此示例的长度和复杂性与其等同的 MXML 版本相比较, 您可以看到 MXML 的简单的基于标签的声明性语法是如何使您免于编写许多 ActionScript 代码行来进行组件布局的。 |
|