// // another example of the Java AWT, showing use of Menu class. // see class definition and main method for details. // import java.awt.* ; import java.awt.event.* ; // // a MenuExample object is a CloseableFrame with a menu bar and a // Label. the menu bar contains two pull-down menus, a // "main" menu with selections resembling those of a "file" // menu, and a "secondary" menu containing two submenus // with selections to enable/disable the elements in the // main menu. when the user makes a selection, corresponding // text is displayed in the Label; if the selection is from // the secondary menu, the corresponding selection in the // main menu is enabled/disabled. // class MenuExample extends CloseableFrame { Label display = new Label("", Label.CENTER) ; MenuExample() { super("MenuExample") ; setLayout(new GridLayout(0,1)) ; setDisplay("ready for input -- hello, world!") ; add(display) ; // ---- "main" menu ---- Menu m1 = new Menu("A file menu") ; // listener for regular items. ActionListener a1 = new ActionListener() { public void actionPerformed(ActionEvent e) { processCommand(e.getActionCommand()) ; } } ; // listener for checkbox items. ItemListener i1 = new ItemListener() { public void itemStateChanged(ItemEvent e) { CheckboxMenuItem c = (CheckboxMenuItem) e.getSource() ; processCheck(c) ; } } ; // menu items m1.add("This is some text in the menu.") ; m1.add(makeMenuItem("Open", a1, "open")) ; m1.addSeparator() ; m1.add(makeMenuItem("Print", a1, "print", 'P')) ; m1.add(makeMenuItem("Save", a1, "save", 'S')) ; m1.addSeparator() ; m1.add(makeCheckboxMenuItem("checkbox 1", i1, "cb1")) ; m1.add(makeCheckboxMenuItem("checkbox 2", i1, "cb2")) ; // ---- "secondary" menu ---- // two submenus to enable/disable events in the main // menu. Menu m2 = new Menu("Change file menu") ; m2.add(makeEnableDisableMenu(m1, true, "Enable menu items") ) ; m2.add(makeEnableDisableMenu(m1, false, "Disable menu items") ) ; // ---- menu bar ---- MenuBar mb = new MenuBar() ; mb.add(m1) ; mb.add(m2) ; setMenuBar(mb) ; // ---- end of setup ---- pack() ; show() ; } // ---- methods to construct menu items ---- MenuItem makeMenuItem(String label, ActionListener l) { MenuItem mi = new MenuItem(label) ; mi.addActionListener(l) ; return mi ; } MenuItem makeMenuItem(String label, ActionListener l, String cmd) { MenuItem mi = makeMenuItem(label, l) ; mi.setActionCommand(cmd) ; mi.setName(cmd) ; return mi ; } MenuItem makeMenuItem(String label, ActionListener l, String cmd, int shortCutKey) { MenuItem mi = makeMenuItem(label, l, cmd) ; mi.setShortcut(new MenuShortcut(shortCutKey)) ; return mi ; } CheckboxMenuItem makeCheckboxMenuItem(String label, ItemListener l, String name) { CheckboxMenuItem ci = new CheckboxMenuItem(label) ; ci.setName(name) ; ci.addItemListener(l) ; return ci ; } // ---- method, adapter class to construct submenus ---- // ---- for secondary menu ---- Menu makeEnableDisableMenu(Menu m, boolean enable, String label) { Menu msub = new Menu(label) ; for (int i = 0 ; i < m.getItemCount() ; i++) { MenuItem mi = m.getItem(i) ; msub.add(makeMenuItem(mi.getLabel(), new EnabledAdapter(mi, enable)) ) ; } return msub ; } class EnabledAdapter implements ActionListener { MenuItem target ; boolean enable ; EnabledAdapter(MenuItem t, boolean e) { target = t ; enable = e ; } public void actionPerformed(ActionEvent e) { target.setEnabled(enable) ; String which = (enable ? "enabling" : "disabling") ; display.setText(which + " item labeled " + target.getLabel() ) ; } } // sets text for display. void setDisplay(String s) { display.setText(s) ; } // "processes" regular menu item (displays its action command). void processCommand(String cmd) { display.setText("processing " + cmd + " command") ; } // "processes" checkbox menu item (displays its state). void processCheck(CheckboxMenuItem c) { String onOff = (c.getState() ? "on" : "off") ; display.setText("setting " + c.getName() + " checkbox " + onOff) ; } // // ---- main --------------------------------------------------- // // no command-line arguments. // public static void main(String[] args) { MenuExample m = new MenuExample() ; } }