1

Flexの学習を始めたばかりです。しかし、私は基本的なコードに悩まされています。Action Script でコンポーネント (ボタンなど) を Application コンテナに追加する方法を確認しようとしています。しかし、出力にボタンが表示されません。以下は私のコードです、

<?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"
             creationComplete="init()">
<fx:Script>
    <![CDATA[

             import mx.controls.Alert;

             import spark.components.Button;
             import spark.layouts.BasicLayout;

             private function init():void{
                 var bl:BasicLayout = new BasicLayout();
                 var app:Application = new Application();
                 var button1:Button = new Button();
                 var button2:Button = new Button();

                 button1.label ="Button one";
                 button1.x = 100;
                 button2.label = "Two";
                 button2.x = 30;
                 layout = bl;
                 addChild(button1);
                 addChild(button2);


             }
    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

私がここでやっている間違いは何ですか?

ありがとう

4

2 に答える 2

4

This code should even throw a runtime error, because you're not allowed to use addChild here.
Since Flex 4 there is a new component set (called Spark). When adding visual items to Spark containers, you must use the addElement method. And since s:Application is a Spark container, you should do it here too. So simply replace addChild with addElement in your code and it will work.

You might ask: if I can't use it, why is "addChild" still there?
Well, the answer is: legacy. All Flex components (i.e. both mx and Spark) inherit from UIComponent, which implements the addChild method for the Flex framework (addChild is actually a pure ActionScript method of the DisplayObjectContainer class). addElement does some additional stuff (I won't get too technical here), which is why you can't use addChild anymore. But addChild is still there (inherited from UIComponent) and will throw an error telling you that you can't use this method.

This is the relevant code from SkinnableComponent:

override public function addChild(child:DisplayObject):DisplayObject
{
    throw(new Error(resourceManager.getString("components", "addChildError")));
}
于 2012-09-17T09:12:55.450 に答える
0

私はBasicLayOutに慣れていませんが、コードを単純に保つとボタンが表示されると思います。

 private function init():void{
            // var bl:BasicLayout = new BasicLayout(); 
            // var app:Application = new Application();
// 1. Create button
             var button1:Button = new Button();
             var button2:Button = new Button();
// 2. customize button
             button1.label ="Button one";
             button1.x = 100;
             button2.label = "Two";
             button2.x = 30;
// 3. add button into the container, 'this' is Applciation
           //  layout = bl;
             addElement(button1); // addChild for Flex3
             addElement(button2);
         }

コードを変更して再試行してください。頑張ってください。

于 2012-09-17T08:19:51.290 に答える