Chapter 11: Using Menu-Based Controls--Menu-based control events

User interaction with a Menu or menu-based control is event-driven; that is, applications typically handle events
generated when the user opens, closes, or selects within a menu, or submenu or rolls over or out of menu items. 
The Menu and MenuBar controls dispatch an identical set of menu-specific events. Event handling with PopUp-
MenuButton controls differs from the other two controls, but shares many elements in common with the others.


Menu控件事件

Menu控件在MenuEvent类中定义了以下和菜单相关的事件类型:
change (MenuEvent.CHANGE) Dispatched when a user changes current menu selection by using the keyboard
or mouse.
itemClick (MenuEvent.ITEM_CLICK) Dispatched when a user selects an enabled menu item of type normal,
check, or radio. This event is not dispatched when a user selects a menu item of type separator, a menu item
that opens a submenu, or a disabled menu item.
itemRollOut (MenuEvent.ITEM_ROLL_OUT) Dispatched when the mouse pointer rolls off of a Menu item.
itemRollOver (MenuEvent.ITEM_ROLL_OVER) Dispatched when the mouse pointer rolls onto a Menu item.
menuHide (MenuEvent.MENU_HIDE) Dispatched when the entire menu or a submenu closes.
menuShow  (MenuEvent.MENU_SHOW) Dispatched when the entire menu or a submenu opens.

传递到事件监听器的event对象类型是MenuEvent,它包含一个或多个以下属性:

Property

Description

item

The item in the data provider for the menu item associated with the event.

index

The index of the item in the menu or submenu that contains it.

label

The label of the item.

menu

A reference to the Menu control where the event occurred.

menuBar

The MenuBar control instance that is the parent of the menu, or undefined when the menu does not belong 

MenuBar control on pa 365.

to a MenuBar. For more information, see 

访问一个基于对象的菜单项中的property和field时,采用如下方式:
ta1.text = event.item.label
To access attributes of an E4X XML-based menu item, you specify the menu item attribute name in E4X syntax,

访问E4X XML-based 的菜单项的attribu的时候,如下方式访问:
ta1.text = event.item.@label

 

注意:如果你在一个菜单控件的子菜单上添加了一个事件监听器,当data provider发生结构改变时(比如:一个element被remove了),那么这个监听器可能就跟着消失了,为了确保当dataprovider发生变化时,事件监听器还好用,要么把事件监听器加在菜单控件上,而不是子菜单上,要么当data provider的结构改变时都重新注册一次。
The following example shows a menu with a simple event listener. 
 


MenuBar事件
The following figure shows a MenuBar control:

MenuBar的事件,比Menu少一个itemclick事件。

change (MenuEvent.CHANGE) Dispatched when a user changes current menu bar selection by using the
keyboard or mouse. This event is also dispatched when the user changes the current menu selection in a pop-up
submenu. When the event occurs on the menu bar, the
menu property of the MenuEvent object is null.
itemRollOut (MenuEvent.ITEM_ROLL_OUT) Dispatched when the mouse pointer rolls off of a menu bar
item.
itemRollOver (MenuEvent.ITEM_ROLL_OVER) Dispatched when the mouse pointer rolls onto a menu bar
item.
menuHide (MenuEvent.MENU_HIDE) Dispatched when a pop-up submenu closes.
menuShow (MenuEvent.MENU_SHOW) Dispatched when a pop-up submenu opens, or the user selects a menu
bar item with no drop-down menu.

Note:当你选择menu bar上的一个item时,MenuBar控件不派发itemClick事件,只有当你选择的是弹出的子菜单中的item时才会派发itemClick事件。 For more information, see Menu control events– on page 354.

对于每个弹出的子菜单,MenuBar控件派发change, itemClick, itemRollOut, itemRollOver, menuShow, and menuHide事件,和Menu控件是一样的,所以Handle events triggered by the pop-up menus as you would handle events from Menu controls.
The following example handles events for the menu bar and for the pop-up submenus.


 

 例子:使用Menu控件事件

     下面的例子是关于Menu控件的事件,它展示了两个菜单,一个data provider是XML,一个是Array。当用户打开菜单,移动鼠标,选择菜单项时,TextArea控件显示了关于每个事件的信息,It shows some of the differences in how you handle XML and object-based menus, and indicates some of the types of information that are available about each Menu event.

 

 


PopUpMenuButton控件事件

      PopUpMenuButton 控件可创建一个 PopUpButton 控件,带有一个主子按钮和一个辅助子按钮。单击辅助(右)子按钮会下拉一个菜单。

      因为PopUpMenuButtonPopUpButton控件的子类,它支持PopUpButton控件的所有事件,当用户单击主子按钮时时,PopUpMenuButton控件派发出一个click (MouseEvent.CLICK)事件。

      当用户单击PopUpMenuButton的主子按钮时,它还派发出一个itemClick(MenuEvent.ITEM_CLICK)事件,该事件包含关于所选菜单项的信息,因此,当用户单击主子按钮或者从下拉菜单中选择一项时都会派发itemClick事件。因为两种情况都会派发同一个事件,所以单击主子按钮产生的行为和在下拉框最后选择的行为是一样的,所以主子按钮扮演着经常被使用的菜单项。

The following example shows how the PopUpMenuButton generates events and how an application can handle
them.
     当用户从弹出的菜单中选择一项时,发生下列事件顺序: The PopUpMenuButton dispatches an itemClick event. The applicationsitemClickHandler() event listener function handles the itemClick event and displays the information about the event in an Alert control.
     当用户单击主子按钮,发生下列事件顺序: The PopUpMenuButton control dispatches a click event. The PopUpMenuButton control dispatches an itemClick event. The applicationsitemClickHandler() event listener function handles the itemClick event and displays information about the selected Menu item in an Alert control. The applicationsclickHandler() event listener function also handles the MouseEvent.CLICK event, and displays the Button label in an Alert control.
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值