简体   繁体   English

flex 4添加按钮

[英]flex 4 add button

i have this code that doesn't show any error but can't work; 我的这段代码没有显示任何错误,但是无法正常工作; can you help me? 你能帮助我吗? i just want to place new button on my form: 我只想在表单上放置新按钮:

minHeight="600">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.controls.Button;
            function onClick() {
                var button2:mx.controls.Button = new mx.controls.Button();
                button2.x = 100;
                button2.y = 100;
                Form1.addChild(button2);
            }
        ]]>
    </fx:Script>
    <s:Form id="Form1" x="0" y="0" width="100%" height="100%">
        <s:Button id="button1" label="button1" click="onClick()" />
    </s:Form>
</s:Application>

There was a runtime exception when I tried to run this. 当我尝试运行此程序时,出现运行时异常。

This should do the trick: 这应该可以解决问题:

protected function onClick():void {
var button2:mx.controls.Button = new mx.controls.Button();
//button2.x = 100; These two lines are obsolete because form handles the layout of its children automatically
//button2.y = 100;
Form1.addElement(button2);
}

With Spark containers you need to do addElement() instead of addChild(); 对于Spark容器,您需要执行addElement()而不是addChild();。

You can find more information about working with spark components in the adobe livedocs at http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf61c8a-7ff9.html 您可以在http://help.adobe.com/zh_CN/flex/using/WS2db454920e96a9e51e63e3d11c0bf61c8a-7ff9.html上的Adobe Livedocs中找到有关使用Spark组件的更多信息。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM