在 Flex 4 中使用 AS3 实例化按钮?

发布于 2024-10-10 09:11:44 字数 454 浏览 0 评论 0原文

我在 Flex 3.5 SDK 和 Flash Builder 4 上使用了这段代码,该代码在 MXML 应用程序的creationComplete 上调用的函数内。

var myButton:Button = new Button;
myButton.label = "test";
addChild(myButton);
Alert.show("Button Created");

然而,它工作正常,当我仅在同一个 Flash Builder 4 上使用它时,在 Flex 4.0 SDK 下,什么也没有发生。 Alert.show() 甚至没有显示,这意味着它甚至没有达到这一点。

所以我的问题是,出了什么问题?我错过了什么吗?

聚苯乙烯 我需要能够动态创建/删除 MXML 组件(在应用程序运行时)。这只是一个测试脚本,我在实现我所需要的方面惨遭失败。

I used this code on Flex 3.5 SDK with Flash Builder 4 inside a function that is being called on creationComplete of the MXML app.

var myButton:Button = new Button;
myButton.label = "test";
addChild(myButton);
Alert.show("Button Created");

It works fine however, when I use it on the same Flash Builder 4 only this time, under Flex 4.0 SDK, nothing's happening. The Alert.show() isn't even showing which means it doesn't even get to that point.

So my question is, what's wrong? Am I missing something?

P.S.
I need to be able to create / remove MXML components on the fly (while the app is running). This is just a test script and I'm failing miserably at achieving what I need.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静被遗忘 2024-10-17 09:11:44

在 Spark 中,您需要使用 addElement 而不是 addChild

var b:Button = new Button();
addElement(b);

我不确定为什么您的警报不起作用:\

In Spark you need to use addElement instead of addChild

var b:Button = new Button();
addElement(b);

I'm not sure why your alert isn't working :\

寄意 2024-10-17 09:11:44

通常,您在 MXML 中创建 UI,但在某些情况下,您确实需要动态创建 UI 元素,正如另一张海报提到的,addElement() 是 Fl​​ex 4 Spark 容器的关键。

将组件添加到 MX 容器(从 Flex 3 开始)时,您仍然在 Flex 4 中使用 addChild()。仅在添加到 Spark 容器时才需要使用 addElement()。

当然,当有类似的 MX 容器时,Adobe 建议您使用 Spark 容器。

Usually you create the UI in MXML, but in some cases you do need to create UI elements on the fly, and as the other poster mentioned, addElement() is the key with Flex 4 Spark containers.

When adding components to MX containers (from Flex 3), you still use addChild() in Flex 4. You only need to use addElement() when adding to Spark containers.

Of course, Adobe recommends you use the Spark containers when there is a somewhat comparable MX container.

雪花飘飘的天空 2024-10-17 09:11:44

最好以灵活的方式进行。

<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        private function alert():void
        {
            Alert.show("Button added to stage");
        }
    ]]>
</fx:Script>

<s:Button id="myButton" label="test" addedToStage="alert()"/>

您不能仅使用 Flex 中的 addChild 添加按钮。首先,您需要创建一个 UIComponent,然后将按钮添加到 UIComponent。和flash的方式有点不一样。

Its better that you do it in the flex way.

<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        private function alert():void
        {
            Alert.show("Button added to stage");
        }
    ]]>
</fx:Script>

<s:Button id="myButton" label="test" addedToStage="alert()"/>

You can't add a button using mere addChild in flex. First you need to create an UIComponent and then add the button to the UIComponent. Its a bit different from the flash way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文