簡體   English   中英

addChild的怪異行為

[英]weird behaviour with addChild

我正在嘗試使用以下代碼向我的應用程序添加一個Box

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600"
                >
    <mx:HBox height="100%" width="100%" backgroundColor="red"  borderColor="black"/>
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.containers.Box;
            import mx.events.FlexEvent;


            protected function button1_clickHandler():void
            {
                var box:Box = new Box();
                box.setStyle("backgroundColor","blue");
                box.height = 100;
                box.width = 100;
                //box.addChild(new Button());
                addChild(box);
                trace("children  "+numChildren);
            }

        ]]>
    </mx:Script>
    <mx:Button label="click" click="button1_clickHandler()" x="200" y="200" />
</mx:Application>

該代碼在flexBuilder中有效。但是在命令提示符下編譯(使用mxmlc命令)無效。 請就這個問題向我提出建議,因為我的工作完全取決於命令提示符。

在此先感謝vengatesh s

這完全取決於您使用的編譯器。 如果您使用的是Flex 4+編譯器,建議您嘗試使用addElement而不是addChild。 上面的相同代碼只是更改為

protected function button1_clickHandler():void
{
    var box:Box = new Box();
    box.setStyle("backgroundColor","blue");
    box.height = 100;
    box.width = 100;
    /********** ----------- CHANGE----------------------********/
    // This is the only change from your code
    addElement(box);
    /********** ----------- CHANGE----------------------********/
    trace("children  "+numChildren);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM