简体   繁体   中英

How can I add a button to the stage in flex using only as3?

I am using the Flex SDK within visual studio and trying to dynamically add a button to the stage. Here is a quick example of what I am doing.

public class Test extends Sprite
{                       
    public function Test()
    {
        init();                 
    }                                                               

    private function init():void
    {

        var btnBrowse:Button = new Button();                                        
        btnBrowse.label = "Browse";
        btnBrowse.x = 0;
        btnBrowse.y = 0;
        btnBrowse.width=100;
        btnBrowse.height=100;           
        addChild(btnBrowse);
    }
}

Nothing seems to show up and the screen is still empty. I am importing mx.controls.* for the button. Could that create an issue since I am not using mxml only as3?

You can't use Flex framework controls in an AS3 Only project. If you are trying to avoid MXML then just create a new Flex Project where the root tag is like:

<FooApplication xmlns="*"/>

And create a new AS3 Class like:

package {

import mx.core.Application;

public class FooApplication extends Application {

// now override something like createChildren to add a button.

}
}

尝试将基类从Sprite更改为Canvas。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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