flex
  • actionscript-3
  • 2008-11-18 40 views 7 likes 
    7

    我想使用Actionscript動態地在Flex中創建帶有圖標的按鈕。在動作中創建一個帶有圖標的按鈕

    我試過了,沒有成功:

    var closeButton = new Button(); 
    closeButton.setStyle("icon", "@Embed(source='images/closeWindowUp.png"); 
    

    回答

    11

    我發現對我的作品的答案。在我的.mxml文件,我創建的圖標類我將使用:

    // Classes for icons 
    [Embed(source='images/closeWindowUp.png')] 
    public static var CloseWindowUp:Class; 
    [Embed(source='/images/Down_Up.png')] 
    public static var Down_Up:Class; 
    [Embed(source='/images/Up_Up.png')] 
    public static var Up_Up:Class; 
    

    在我的應用程序的ActionScript部分,我使用這些類動態創建按鈕時:

    var buttonHBox:HBox = new HBox(); 
    var closeButton:Button = new Button(); 
    var upButton:Button = new Button(); 
    var downButton:Button = new Button(); 
    
    closeButton.setStyle("icon", SimpleWLM.CloseWindowUp); 
    buttonHBox.addChild(closeButton); 
    
    upButton.setStyle("icon", SimpleWLM.Up_Up); 
    buttonHBox.addChild(upButton); 
    
    downButton.setStyle("icon", SimpleWLM.Down_Up); 
    buttonHBox.addChild(downButton); 
    
    0

    我假設你將它添加到舞臺?

    另外,我認爲您的Embed缺少一個關閉引號/ paren。

    closeButton.setStyle("icon", "@Embed(source='images/closeWindowUp.png"); 
    

    應該是:

    closeButton.setStyle("icon", "@Embed(source='images/closeWindowUp.png')"); 
    
    +0

    添加缺少的關閉引號/ paren沒有任何區別。 我仍然遇到運行時錯誤 類型強制失敗:無法將「@Embed(source ='images/closeWindowUp.png')」轉換爲Class。我打電話給 buttonHBox.addChild(closeButton); 這是你加入舞臺的意思嗎? – 2008-11-18 23:58:49

    2

    的錯誤是在報價,應該有@Embed周圍沒有引號:

    closeButton.setStyle("icon", @Embed(source="images/closeWindowUp.png")); 
    
    +0

    對我來說,這只是給出了以下錯誤:`1041:屬性不可調用.`。不過,我正在使用Flex 3。 – edam 2014-07-02 14:00:41

    3

    您可以使用按鈕圖標的動態變化的這一個選項。

    嵌入你的圖標

    [Embed(source='com/images/play.png')] 
    [Bindable] 
    public var imagePlay:Class; 
    
    [Embed(source='com/images/pause.png')] 
    [Bindable] 
    public var imagePause:Class; 
    

    使用一個按鈕來切換視頻的播放和暫停

    private function playpause():void 
    { 
        if (seesmicVideo.playing) 
        { 
         seesmicVideo.pause(); 
         btn_play.setStyle("icon",imagePlay); 
        } 
        else 
        { 
         seesmicVideo.play(); 
         btn_play.setStyle("icon",imagePause); 
        } 
    }   
    
    1

    我能在我的按鈕使用的圖標,下面的代碼:

    <mx:Button id="buttonPlay" label="Play" click="playButtonClicked();" enabled="false" icon="@Embed('./play.png')"/> 
    

    play.png文件位於mxml文件的同一個文件夾中。

    我正在使用Flash Builder版本4.6。

    編輯:問題是關於ActionScript而不是MXML,但我留下這個答案僅供參考。

    相關問題