java.awt

Class MenuItem

  • All Implemented Interfaces:
    Serializable, Accessible
    Direct Known Subclasses:
    CheckboxMenuItem, Menu

    public class MenuItem
    extends MenuComponent
    implements Accessible
    All items in a menu must belong to the class MenuItem, or one of its subclasses.

    The default MenuItem object embodies a simple labeled menu item.

    This picture of a menu bar shows five menu items: The following text describes this graphic.
    The first two items are simple menu items, labeled "Basic" and "Simple". Following these two items is a separator, which is itself a menu item, created with the label "-". Next is an instance of CheckboxMenuItem labeled "Check". The final menu item is a submenu labeled "More Examples", and this submenu is an instance of Menu.

    When a menu item is selected, AWT sends an action event to the menu item. Since the event is an instance of ActionEvent, the processEvent method examines the event and passes it along to processActionEvent. The latter method redirects the event to any ActionListener objects that have registered an interest in action events generated by this menu item.

    Note that the subclass Menu overrides this behavior and does not send any event to the frame until one of its subitems is selected.

    See Also:
    Serialized Form
    • Constructor Detail

      • public MenuItem(String label)
                 throws HeadlessException
        Constructs a new MenuItem with the specified label and no keyboard shortcut. Note that use of "-" in a label is reserved to indicate a separator between menu items. By default, all menu items except for separators are enabled.
        Parameters:
        label - the label for this menu item.
        Throws:
        HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
        Since:
        JDK1.0
        See Also:
        GraphicsEnvironment.isHeadless()
      • public MenuItem(String label,
                MenuShortcut s)
                 throws HeadlessException
        Create a menu item with an associated keyboard shortcut. Note that use of "-" in a label is reserved to indicate a separator between menu items. By default, all menu items except for separators are enabled.
        Parameters:
        label - the label for this menu item.
        s - the instance of MenuShortcut associated with this menu item.
        Throws:
        HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
        Since:
        JDK1.1
        See Also:
        GraphicsEnvironment.isHeadless()
    • Method Detail

      • addNotify

        public void addNotify()
        Creates the menu item's peer. The peer allows us to modify the appearance of the menu item without changing its functionality.
      • getLabel

        public String getLabel()
        Gets the label for this menu item.
        Returns:
        the label of this menu item, or null if this menu item has no label.
        Since:
        JDK1.0
        See Also:
        setLabel(java.lang.String)
      • setLabel

        public void setLabel(String label)
        Sets the label for this menu item to the specified label.
        Parameters:
        label - the new label, or null for no label.
        Since:
        JDK1.0
        See Also:
        getLabel()
      • isEnabled

        public boolean isEnabled()
        Checks whether this menu item is enabled.
        Since:
        JDK1.0
        See Also:
        setEnabled(boolean)
      • setEnabled

        public void setEnabled(boolean b)
        Sets whether or not this menu item can be chosen.
        Parameters:
        b - if true, enables this menu item; if false, disables it.
        Since:
        JDK1.1
        See Also:
        isEnabled()
      • enable

        @Deprecated
        public void enable()
        Deprecated. As of JDK version 1.1, replaced by setEnabled(boolean).
      • enable

        @Deprecated
        public void enable(boolean b)
        Deprecated. As of JDK version 1.1, replaced by setEnabled(boolean).
      • disable

        @Deprecated
        public void disable()
        Deprecated. As of JDK version 1.1, replaced by setEnabled(boolean).
      • getShortcut

        public MenuShortcut getShortcut()
        Get the MenuShortcut object associated with this menu item,
        Returns:
        the menu shortcut associated with this menu item, or null if none has been specified.
        Since:
        JDK1.1
        See Also:
        setShortcut(java.awt.MenuShortcut)
      • setShortcut

        public void setShortcut(MenuShortcut s)
        Set the MenuShortcut object associated with this menu item. If a menu shortcut is already associated with this menu item, it is replaced.
        Parameters:
        s - the menu shortcut to associate with this menu item.
        Since:
        JDK1.1
        See Also:
        getShortcut()
      • deleteShortcut

        public void deleteShortcut()
        Delete any MenuShortcut object associated with this menu item.
        Since:
        JDK1.1
      • enableEvents

        protected final void enableEvents(long eventsToEnable)
        Enables event delivery to this menu item for events to be defined by the specified event mask parameter

        Since event types are automatically enabled when a listener for that type is added to the menu item, this method only needs to be invoked by subclasses of MenuItem which desire to have the specified event types delivered to processEvent regardless of whether a listener is registered.

        Parameters:
        eventsToEnable - the event mask defining the event types
        Since:
        JDK1.1
        See Also:
        processEvent(java.awt.AWTEvent), disableEvents(long), Component.enableEvents(long)
      • setActionCommand

        public void setActionCommand(String command)
        Sets the command name of the action event that is fired by this menu item.

        By default, the action command is set to the label of the menu item.

        Parameters:
        command - the action command to be set for this menu item.
        Since:
        JDK1.1
        See Also:
        getActionCommand()
      • getListeners

        public <T extends EventListener> T[] getListeners(Class<T> listenerType)
        Returns an array of all the objects currently registered as FooListeners upon this MenuItem. FooListeners are registered using the addFooListener method.

        You can specify the listenerType argument with a class literal, such as FooListener.class. For example, you can query a MenuItem m for its action listeners with the following code:

        ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));
        If no such listeners exist, this method returns an empty array.
        Parameters:
        listenerType - the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
        Returns:
        an array of all objects registered as FooListeners on this menu item, or an empty array if no such listeners have been added
        Throws:
        ClassCastException - if listenerType doesn't specify a class or interface that implements java.util.EventListener
        Since:
        1.3
        See Also:
        getActionListeners()
      • processEvent

        protected void processEvent(AWTEvent e)
        Processes events on this menu item. If the event is an instance of ActionEvent, it invokes processActionEvent, another method defined by MenuItem.

        Currently, menu items only support action events.

        Note that if the event parameter is null the behavior is unspecified and may result in an exception.

        Overrides:
        processEvent in class MenuComponent
        Parameters:
        e - the event
        Since:
        JDK1.1
        See Also:
        processActionEvent(java.awt.event.ActionEvent)
      • processActionEvent

        protected void processActionEvent(ActionEvent e)
        Processes action events occurring on this menu item, by dispatching them to any registered ActionListener objects. This method is not called unless action events are enabled for this component. Action events are enabled when one of the following occurs:

        • An ActionListener object is registered via addActionListener.
        • Action events are enabled via enableEvents.

        Note that if the event parameter is null the behavior is unspecified and may result in an exception.

        Parameters:
        e - the action event
        Since:
        JDK1.1
        See Also:
        ActionEvent, ActionListener, enableEvents(long)
      • paramString

        public String paramString()
        Returns a string representing the state of this MenuItem. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.
        Overrides:
        paramString in class MenuComponent
        Returns:
        the parameter string of this menu item
      • getAccessibleContext

        public AccessibleContext getAccessibleContext()
        Gets the AccessibleContext associated with this MenuItem. For menu items, the AccessibleContext takes the form of an AccessibleAWTMenuItem. A new AccessibleAWTMenuItem instance is created if necessary.
        Specified by:
        getAccessibleContext in interface Accessible
        Overrides:
        getAccessibleContext in class MenuComponent
        Returns:
        an AccessibleAWTMenuItem that serves as the AccessibleContext of this MenuItem
        Since:
        1.3

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 11/06/2005 gemaakt, de laatste keer de 04/03/2020 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/java-api-rf-java/awt/MenuItem.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:fr Manuel PHP : https://docs.oracle.com

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut