/* * =============================================================== * DemoMenuItem.java : Create menu bar (file, edit, option, guide) * together with pull-down submenus. * * Adapted from : Pantham S., Pure JFC Swing, 1999. * Modified by : Mark Austin March, 2001 * =============================================================== */ import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class DemoMenuItem extends JApplet { public void init() { // 1. Get the handle on the applet's content pane. Container container = this.getContentPane(); // 2. Add a menu bar to the applet. JMenuBar menuBar = new JMenuBar(); menuBar.setBorder( new BevelBorder(BevelBorder.RAISED) ); menuBar.setBorderPainted(true); container.add(menuBar, BorderLayout.NORTH); // 3. Add the File menu and its menu items . JMenu fileMenu = new JMenu("File", true); menuBar.add(fileMenu); // 4. Add the submenus to the File menu. fileMenu.add(new JMenuItem("New")); fileMenu.add(new JMenuItem("Open")); fileMenu.addSeparator(); fileMenu.add(new JMenuItem("Save")); fileMenu.add(new JMenuItem("Sava As")); fileMenu.addSeparator(); fileMenu.addSeparator(); // 5. Add the Edit menu and its menu items. JMenu editMenu = new JMenu("Edit"); menuBar.add(editMenu); // 6. Add the submenus to the edit menu. editMenu.add(new JMenuItem("Undo")); editMenu.addSeparator(); editMenu.add(new JMenuItem("Cut")); editMenu.add(new JMenuItem("Copy")); editMenu.add(new JMenuItem("Paste")); // 7. Create and add the Options menu and submenus // and their items. JMenu optionsMenu = new JMenu("Options"); menuBar.add(optionsMenu); // 8. Add the submenus to the Options menu. JMenu bookMarksMenu = new JMenu("Book Marks"); optionsMenu.add(bookMarksMenu); // 9. Add the submenus to the Book Marks menu. JMenuItem addMI = new JMenuItem("Add Alt-K"); bookMarksMenu.add(addMI); JMenuItem editMI = new JMenuItem("Edit Alt-B"); bookMarksMenu.add(editMI); JMenu guideMenu = new JMenu("Guide"); bookMarksMenu.add(guideMenu); // 10. Add the submenus to the Guide menu. JMenuItem whatIsNewMI = new JMenuItem("What's New"); whatIsNewMI.setMnemonic('N'); guideMenu.add(whatIsNewMI); JMenuItem whatIsCoolMI = new JMenuItem("What's Cool"); whatIsCoolMI.setMnemonic('C'); guideMenu.add(whatIsCoolMI); // 11. Finally, add two more submenus to the Options menu. JMenuItem javaConsoleMI = new JMenuItem("Java Console"); optionsMenu.add(javaConsoleMI); JMenuItem addressBookMI = new JMenuItem("Address Book"); optionsMenu.add(addressBookMI); } }