Actionscript 3 使用动作脚本动态添加组件

Actionscript 3 使用动作脚本动态添加组件,actionscript-3,apache-flex,Actionscript 3,Apache Flex,刚开始学习Flex。但是我被基本的代码迷住了。我试图检查如何通过操作脚本将组件(比如按钮)添加到应用程序容器中。但是我看不到输出中的按钮。下面是我的代码 <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" x

刚开始学习Flex。但是我被基本的代码迷住了。我试图检查如何通过操作脚本将组件(比如按钮)添加到应用程序容器中。但是我看不到输出中的按钮。下面是我的代码

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
             creationComplete="init()">
<fx:Script>
    <![CDATA[

             import mx.controls.Alert;

             import spark.components.Button;
             import spark.layouts.BasicLayout;

             private function init():void{
                 var bl:BasicLayout = new BasicLayout();
                 var app:Application = new Application();
                 var button1:Button = new Button();
                 var button2:Button = new Button();

                 button1.label ="Button one";
                 button1.x = 100;
                 button2.label = "Two";
                 button2.x = 30;
                 layout = bl;
                 addChild(button1);
                 addChild(button2);


             }
    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

我在这里犯了什么错误


谢谢

我不熟悉BasicLayOut,但如果代码保持简单,我想您可以看到按钮:

 private function init():void{
            // var bl:BasicLayout = new BasicLayout(); 
            // var app:Application = new Application();
// 1. Create button
             var button1:Button = new Button();
             var button2:Button = new Button();
// 2. customize button
             button1.label ="Button one";
             button1.x = 100;
             button2.label = "Two";
             button2.x = 30;
// 3. add button into the container, 'this' is Applciation
           //  layout = bl;
             addElement(button1); // addChild for Flex3
             addElement(button2);
         }

修改您的代码并重试,祝您好运

我不熟悉BasicLayOut,但我认为如果代码保持简单,您可以看到按钮:

 private function init():void{
            // var bl:BasicLayout = new BasicLayout(); 
            // var app:Application = new Application();
// 1. Create button
             var button1:Button = new Button();
             var button2:Button = new Button();
// 2. customize button
             button1.label ="Button one";
             button1.x = 100;
             button2.label = "Two";
             button2.x = 30;
// 3. add button into the container, 'this' is Applciation
           //  layout = bl;
             addElement(button1); // addChild for Flex3
             addElement(button2);
         }

修改您的代码并重试,祝您好运

此代码甚至应该引发运行时错误,因为不允许在此处使用
addChild

自从Flex4以来,有一个新的组件集(称为
Spark
)。将可视项目添加到Spark容器时,必须使用
addElement
方法。由于
s:Application
是一个Spark容器,所以您也应该在这里执行。因此,只需将代码中的
addChild
替换为
addElement
,它就可以工作了

你可能会问:如果我不能使用它,为什么“addChild”还在那里?
答案是:遗产。所有Flex组件(即mx和Spark)都继承自,它为Flex框架实现了
addChild
方法(
addChild
实际上是该类的纯ActionScript方法)
addElement
做了一些额外的东西(我在这里不会太专业),这就是为什么你不能再使用
addChild
。但是
addChild
仍然存在(继承自
UIComponent
),并且将抛出一个错误,告诉您不能使用此方法

这是以下文件中的相关代码:


此代码甚至应该引发运行时错误,因为不允许在此处使用
addChild

自从Flex4以来,有一个新的组件集(称为
Spark
)。将可视项目添加到Spark容器时,必须使用
addElement
方法。由于
s:Application
是一个Spark容器,所以您也应该在这里执行。因此,只需将代码中的
addChild
替换为
addElement
,它就可以工作了

你可能会问:如果我不能使用它,为什么“addChild”还在那里?
答案是:遗产。所有Flex组件(即mx和Spark)都继承自,它为Flex框架实现了
addChild
方法(
addChild
实际上是该类的纯ActionScript方法)
addElement
做了一些额外的东西(我在这里不会太专业),这就是为什么你不能再使用
addChild
。但是
addChild
仍然存在(继承自
UIComponent
),并且将抛出一个错误,告诉您不能使用此方法

这是以下文件中的相关代码:


也许你应该告诉我们什么不起作用?也许你应该告诉我们什么不起作用?谢谢。你是对的。我在调试模式下检查了它,并明确表示使用addElement而不是addChild。但是我不知道为什么,现在从你的回答中知道了原因。值得补充的是,UIComponent
addChild()
方法在这一点上并非完全无用。对它的孩子们来说,这是一个挑战。但您可以使用UIComponent将精灵或其子项添加到Flex UI,这是使用UIComponent子项无法实现的。谢谢。你是对的。我在调试模式下检查了它,并明确表示使用addElement而不是addChild。但是我不知道为什么,现在从你的回答中知道了原因。值得补充的是,UIComponent
addChild()
方法在这一点上并非完全无用。对它的孩子们来说,这是一个挑战。但您可以使用UIComponent将精灵或其子项添加到Flex UI,而使用UIComponent子项是不可能的。