Advanced
Java
Services
|
MenuItem und CheckboxMenuItem |
Konstruktoren | |
MenuItem() | Constructs a new MenuItem with an empty label and no keyboard shortcut |
MenuItem(String label) |
Constructs a new MenuItem with the specified label and no keyboard shortcut. |
MenuItem(String label, MenuShortcut s) | Create a menu item with an associated keyboard shortcut. |
Methoden | |
Returntyp | Name der Methode |
void | addActionListener(ActionListener l) Adds the specified action listener to receive action events from this menu item. |
void | removeActionListener(ActionListener l) Removes the specified action listener so it no longer receives action events from this menu item. |
ActionListener[] | getActionListeners() Returns an array of all the action listeners registered on this menu item. |
String | getLabel() Gets the label for this menu item. |
void | setLabel(String label) Sets the label for this menu item to the specified label. |
boolean | isEnabled() Checks whether this menu item is enabled. |
void | setEnabled(boolean b) Sets whether or not this menu item can be chosen. |
String | getActionCommand() Gets the command name of the action event that is fired by this menu item. |
void | setActionCommand(String command) Sets the command name of the action event that is fired by this menu item. |
void | deleteShortcut() Delete any MenuShortcut object associated with this menu item. |
MenuShortcut | getShortcut() Get the MenuShortcut object associated with this menu item. |
void | setShortcut(MenuShortcut s) Set the MenuShortcut object associated with this menu item. |
Konstruktoren | |
CheckboxMenuItem() | Create a check box menu item with an empty label. |
CheckboxMenuItem(String label) | Create a check box menu item with the specified label. |
CheckboxMenuItem(String label, boolean state) | Create a check box menu item with the specified label and state. |
Methoden | |
Returntyp | Name der Methode |
void | addItemListener(ItemListener l) Adds the specified item listener to receive item events from this check box menu item. |
void | removeItemListener(ItemListener l) Removes the specified item listener so that it no longer receives item events from this check box menu item. |
ItemListener[] | getItemListeners() Returns an array of all the item listeners registered on this checkbox menuitem. |
boolean | getState() Determines whether the state of this check box menu item is "on" or "off." |
void | setState(boolean b) Sets this check box menu item to the specifed state. |
// Frame mit einer MenuBar ausstatten MenuBar mb = new MenuBar(); Frame f = new Frame(); f.setMenuBar(mb); // Menu anlegen und aufnehmen Menu dateiMenu = new Menu("Datei"); mb.add(dateiMenu); // Menupunkte (MenuItems) erstellen MenuItem openItem = new MenuItem("Öffnen") ; MenuItem exitItem = new MenuItem("Beenden") ; // MenuItems in Menu aufnehmen dateiMenu.add(openItem); dateiMenu.add(exitItem); // zweites Menu anlegen und aufnehmen Menu ansichtMenu = new Menu("Ansicht"); mb.add(ansichtMenu); // Menupunkt (CheckboxMenuItem) für zweites Menu erstellen CheckboxMenuItem ansichtItem = new CheckboxMenuItem("Statuszeile", true) ; // CheckboxMenuItem in zweites Menu aufnehmen ansichtMenu.add(ansichtItem);
Reaktion auf Items
Ein MenuItem sendet Actionereignisse und man reagiert mit einem ActionListener auf diese Ereignisse.
Ein CheckboxMenuItem sendet Itemereignisse und man reagiert mit einem ItemListener auf diese Ereignisse.
Einzelheiten hierzu findet man im Kapitel Eventhandling.