使用 ActionScript 3.0 组件 |
|
|
|
| 使用 UI 组件 > 使用 Button 组件 > 创建具有 Button 组件的应用程序 | |||
以下过程解释了如何在创作时将 Button 组件添加到应用程序。在此示例中,单击 Button 时会更改 ColorPicker 组件的状态。
创建具有 Button 组件的应用程序:
aCp.visible = false;
aButton.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
switch(event.currentTarget.label) {
case "Show":
aCp.visible = true;
aButton.label = "Disable";
break;
case "Disable":
aCp.enabled = false;
aButton.label = "Enable";
break;
case "Enable":
aCp.enabled = true;
aButton.label = "Hide";
break;
case "Hide":
aCp.visible = false;
aButton.label = "Show";
break;
}
}
第二行代码将函数 clickHandler() 注册为 MouseEvent.CLICK 事件的事件处理函数。用户单击 Button 时,事件将发生,从而使 clickHandler() 函数根据 Button 的值执行以下操作之一:
以下过程使用 ActionScript 创建一个切换 Button,并且当单击该 Button 时在"输出"面板中显示事件类型。该示例通过调用类的构造函数创建一个 Button 实例,并通过调用 addChild() 方法将该实例添加到舞台上。
使用 ActionScript 代码创建 Button:此操作将组件添加到库中,但不会在应用程序中显示它。
import fl.controls.Button; var aButton:Button = new Button(); addChild(aButton); aButton.label = "Click me"; aButton.toggle = true; aButton.move(50, 50);
move() 方法将按钮放在舞台的 50(x 坐标), 50(y 坐标)位置。
aButton.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
trace("Event type: " + event.type);
}
单击按钮时,Flash 会在"输出"面板中显示消息"事件类型: 单击"。
|
|
|
|