FileDocCategorySizeDatePackage
MenuShortcuts.javaAPI DocExample2300Sat Nov 25 12:55:16 GMT 2000None

MenuShortcuts

public class MenuShortcuts extends JFrame implements ActionListener
Demonstrate JMenu shortcuts and accelerators.
author
Ian Darwin, ian@darwinsys.com
version
$Id: MenuShortcuts.java,v 1.3 2000/11/25 17:55:17 ian Exp $

Fields Summary
JMenuBar
mb
The menubar
JMenu
fm
File, Options, Help
JMenu
om
JMenu
hm
JMenu
opSubm
Options Sub-JMenu
JMenuItem
exitItem
The JMenuItem for exiting.
Constructors Summary
MenuShortcuts(String s)

		super("JMenuShortcuts: " + s);
		mb = new JMenuBar();
		setJMenuBar(mb);		// Frame implements JMenuContainer

		Container cp = getContentPane();
		JMenuItem mi;
		// The File JMenu...
		fm = new JMenu("File");
		fm.setMnemonic('F");
			fm.add(mi = new JMenuItem("Open", 'O"));
			mi.addActionListener(this);
			fm.add(mi = new JMenuItem("Close", 'W"));
			mi.addActionListener(this);
			fm.addSeparator();

			fm.add(mi = new JMenuItem("Print", 'P"));
			mi.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_R, Event.ALT_MASK));

			mi.addActionListener(this);
			fm.addSeparator();
			fm.add(mi = new JMenuItem("Exit", 'Q"));
			exitItem = mi;			// save for action handler
			mi.addActionListener(this);
		mb.add(fm);

		// The Options JMenu...
		om = new JMenu("Options");
			om.add(new JMenuItem("Alpha"));
			om.add(new JMenuItem("Gamma"));
			om.add(new JMenuItem("Delta"));
		mb.add(om);

		// The Help JMenu...
		hm = new JMenu("Help");
			hm.add(mi = new JMenuItem("About"));
			mi.addActionListener(this);
			hm.add(mi = new JMenuItem("Topics"));
			mi.addActionListener(this);
		mb.add(hm);
		// mb.setHelpMenu(hm);	// needed for portability (Motif, etc.).

		// the main window
		cp.add(new MyLabel("Menu Demo Window", 200, 150));
		pack();
	
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent evt)
Handle action events.

		// System.out.println("Event " + evt);
		String cmd;
		if ((cmd = evt.getActionCommand()) == null)
			System.out.println("You chose a menu shortcut");
		else
			System.out.println("You chose " + cmd);
		Object cmp = evt.getSource();
		// System.out.println("Source " + cmp);
		if (cmp == exitItem)
			System.exit(0);
	
public static voidmain(java.lang.String[] arg)

		new MenuShortcuts("Testing 1 2 3...").setVisible(true);