为什么我不能将控制栏添加到我的面板

时间:2012-03-07 11:46:29

标签: actionscript-3 flex

var panel:Panel = new Panel();
panel.title = source.displayValue + "-" + data.displayValue;
panel.percentWidth = 100;

var upButton:Button = new Button();
//  upButton.setStyle("icon", upIcon);

upButton.label="test";


var downButton:Button = new Button();
var closeButton:Button = new Button();

var controlBar:ControlBar = new ControlBar();
controlBar.addChild(upButton);
controlBar.addChild(downButton);
controlBar.addChild(closeButton);

controlBar.height = 50;

//panel.addChild(chart);
panel.addChild(controlBar);

我有上面的代码,看看我能在网上做的所有例子我已经做好了一切,但是,我仍然没有我的控制栏

当检查每个元素在eclipse中的子元素数时,控制条numChildren属性会上升,但是面板不会如此,就好像它没有被添加一样。请帮忙:|

2 个答案:

答案 0 :(得分:1)

如果您使用的是flex 4,则可以在面板上指定controlBarContent属性:

<?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">

    <fx:Script>
        <![CDATA[
            import spark.components.Panel;

            protected function button_clickHandler(event:MouseEvent):void
            {
                var panel:Panel = new Panel();
                panel.title = "Hello Panel";
                panel.percentWidth = 100;

                var upButton:Button = new Button();
                upButton.label="upButton";
                var downButton:Button = new Button();
                downButton.label="downButton";
                var closeButton:Button = new Button();
                closeButton.label="closeButton";

                panel.controlBarContent = [upButton, downButton, closeButton];
                addElement(panel);
            }
        ]]>
    </fx:Script>

    <s:Button label="createPanel" click="button_clickHandler(event)" />
</s:Application>

希望有所帮助。

答案 1 :(得分:1)

我之前回答的Flex 3版本:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" minWidth="955" minHeight="600">

    <mx:Script>
        <![CDATA[
            import mx.containers.ControlBar;
            import mx.containers.Panel;
            import mx.containers.VBox;
            import mx.controls.Label;

            protected function button_clickHandler(event:MouseEvent):void
            {
                var panel:Panel = new Panel();
                panel.title = "Hello Panel";
                panel.percentWidth = 100;

                var upButton:Button = new Button();
                upButton.label="upButton";
                var downButton:Button = new Button();
                downButton.label="downButton";
                var closeButton:Button = new Button();
                closeButton.label="closeButton";

                var controlBar:ControlBar = new ControlBar();
                controlBar.addChild(upButton);
                controlBar.addChild(downButton);
                controlBar.addChild(closeButton);

                var vBox:VBox = new VBox();
                vBox.percentWidth = 100;
                vBox.height = 100;
                var lbl:Label = new Label();
                lbl.text = "This is content";
                vBox.addChild(lbl);
                panel.addChild(vBox);

                panel.addChild(controlBar);
                addChild(panel);
            }
        ]]>
    </mx:Script>

    <mx:Button top="" label="createPanel" click="button_clickHandler(event)" />

</mx:Application>