【问题标题】:Change label of a NativeMenuItem更改 NativeMenuItem 的标签
【发布时间】:2009-03-13 11:08:08
【问题描述】:

我正在 Flex Builder 中试验 Adob​​e AIR 和 Google Maps API。问题是,我正在制作一个 NativeMenu,并且想知道,当舞台处于全屏状态时,如何将“全屏”项目的标签更改为“退出全屏”?

如果您在代码中看到任何可以/应该写得更好的东西,请告诉我 ;)

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication 
xmlns:mx="http://www.adobe.com/2006/mxml" 
layout="absolute"
height="600"
width="700"
minHeight="100"
minWidth="100"
showStatusBar="false"
title="Gmaps 0.002"
>
<maps:Map 
xmlns:maps="com.google.maps.*" 
id="map" 
mapevent_mapready="onMapReady(event)" 
width="100%" 
height="100%" 
url="" 
key=""
/>

   <mx:Button id="fullscreenButton" click="toggleFullScreen()"/>
<mx:Script>
<![CDATA[


import com.google.maps.LatLng;
import com.google.maps.Map;
import com.google.maps.MapEvent;
import com.google.maps.MapType;
import com.google.maps.MapMouseEvent;
import com.google.maps.controls.MapTypeControl;
import com.google.maps.controls.ZoomControl;
import com.google.maps.controls.PositionControl;
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;
import flash.events.Event;
import mx.core.Window;
import flash.display.StageDisplayState;






private function onMapReady(event:Event):void {
  map.setCenter(new LatLng(59.908165,10.742719), 14, MapType.NORMAL_MAP_TYPE);


        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
    function keyDownHandler(event:KeyboardEvent):void {
    if (event.keyCode == 70) {
        toggleFullScreen();
        }
    }

    createMenu();

  map.enableScrollWheelZoom();
  map.enableContinuousZoom();
  map.enableControlByKeyboard();



   systemManager.stage.addEventListener(FullScreenEvent.FULL_SCREEN, fullScreenHandler);





  map.addEventListener(MapMouseEvent.ROLL_OVER, function(event:MapMouseEvent):void {
    map.addControl(new ZoomControl());
    map.addControl(new PositionControl());
    map.addControl(new MapTypeControl());
  });


  map.addEventListener(MapMouseEvent.ROLL_OUT, function(event:MapMouseEvent):void {
    map.removeControl(new ZoomControl());
    map.removeControl(new PositionControl());
    map.removeControl(new MapTypeControl());
  });





}



private function createMenu():void{
      var mainMenu:NativeMenu = new NativeMenu();
            var fullscreenMenu:NativeMenuItem = new NativeMenuItem("Fullscreen");
            var maximizeMenu:NativeMenuItem = new NativeMenuItem("Maximize");
            var restoreMenu:NativeMenuItem = new NativeMenuItem("Restore");
            var separatorA:NativeMenuItem = new NativeMenuItem("A", true); 
            var closeMenu:NativeMenuItem = new NativeMenuItem("Close");
            fullscreenMenu.addEventListener(Event.SELECT, handleMenuClick);
            maximizeMenu.addEventListener(Event.SELECT, handleMenuClick);
            restoreMenu.addEventListener(Event.SELECT, handleMenuClick);
            closeMenu.addEventListener(Event.SELECT, handleMenuClick);
            mainMenu.addItem(fullscreenMenu);
            mainMenu.addItem(maximizeMenu);
            mainMenu.addItem(restoreMenu);
            mainMenu.addItem(separatorA);
            mainMenu.addItem(closeMenu);

            //fullscreenMenu.enabled = false;
            //fullscreenMenu.label = "Test";







            this.contextMenu=mainMenu;
     }

     private function handleMenuClick(e:Event):void{
      var menuItem:NativeMenuItem = e.target as NativeMenuItem;
            if(menuItem.label == "Fullscreen") toggleFullScreen();
            if(menuItem.label == "Maximize") this.maximize();
            if(menuItem.label == "Restore") this.restore();
            if(menuItem.label == "Close") this.close();
     }





     private function fullScreenHandler(evt:FullScreenEvent):void {
  if (evt.fullScreen) {
    //fullscreenMenu.label = "Test";

  } else {

  }
}



     private function toggleFullScreen():void {
  try {
    switch (systemManager.stage.displayState) {
      case StageDisplayState.FULL_SCREEN_INTERACTIVE:
        systemManager.stage.displayState = StageDisplayState.NORMAL;
        break;
      default:
        systemManager.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
        break;
    }
  } catch (err:SecurityError) {
  // ignore
  }
}





]]>
  </mx:Script>
  </mx:WindowedApplication>

【问题讨论】:

    标签: apache-flex flash actionscript-3 air


    【解决方案1】:

    为了将标签更改为 NativeMenu,你可以试试这个

    yourNativeMenu.getItemAt(0).label="new_label";
    

    其中getItemAt(0) 返回 NativeMenu 中的第一项

    【讨论】:

    • 当我添加这个时: mainMenu.getItemAt(0).label="Exit fullscreen";到私有函数fullScreenHandler,我收到此错误“严重性和描述路径资源位置创建时间ID 1120:访问未定义属性mainMenu.Gmaps/src Gmaps.mxml line 138 1236943043953 331”。有什么想法吗?
    • 你应该将“mainMenu”var设置为实例变量,你也可以尝试通过“this.contextMenu”访问mainMenu
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多