/* * File: FrameMenus.java * * This applet/application demonstrates the following classes: * TextArea, Frame, MenuBar, Menu, MenuItem, CheckboxMenuItem, * and MenuShortcut. * * Note that MenuItems generate ActionEvents, while CheckboxMenuItems * generate ItemEvents. * */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class FrameMenus extends Applet implements ActionListener { private Button button; public void init() { button = new Button( "New Web Browser" ); add( button ); button.addActionListener( this ); } public void actionPerformed( ActionEvent e ) { new NewWebBrowser(); } // This applet also runs as an application: public static void main( String[] args ) { new NewWebBrowser(); } } // Simulate a Netscape web browser window: class NewWebBrowser extends Frame { protected static int num_windows = 0; // number of open windows String ellipsis = "..."; // ellipsis for menu items // char ellipsis = '\u2026'; // Why doesn't this work? // GUI components: MenuBar menubar; TextArea textarea; // Constructor: public NewWebBrowser() { // Invoke the constructor of the superclass: super( "Web Browser" ); num_windows++; // update window counter // Set frame parameters: setSize( 700, 800 ); setFont( new Font( "SansSerif", Font.BOLD, 12 ) ); textarea = new TextArea(); add( textarea, BorderLayout.CENTER ); textarea.setFont( new Font( "Monospaced", Font.BOLD, 10 ) ); menubar = new MenuBar(); menubar.add( new FileMenu() ); menubar.add( new EditMenu() ); menubar.add( new ViewMenu() ); menubar.add( new GoMenu() ); menubar.add( new BookmarksMenu() ); menubar.add( new OptionsMenu() ); menubar.add( new DirectoryMenu() ); menubar.add( new WindowMenu() ); menubar.setHelpMenu( new HelpMenu() ); // the Help menu is special setMenuBar( menubar ); setVisible( true ); } class FileMenu extends Menu implements ActionListener { // Menu item names: MenuItem newWebBrowserItem, newMailMessageItem, mailDocumentItem, openLocationItem, openFileItem, saveAsItem, uploadFileItem, printItem, closeItem, exitItem; // Menu constructor: public FileMenu() { // Invoke the constructor of the superclass: super( "File" ); // Menu items with shortcuts: newWebBrowserItem = new MenuItem( "New Web Browser", new MenuShortcut( KeyEvent.VK_N ) ); newMailMessageItem = new MenuItem( "New Mail Message", new MenuShortcut( KeyEvent.VK_M ) ); mailDocumentItem = new MenuItem( "Mail Document" ); openLocationItem = new MenuItem( "Open Location" + ellipsis, new MenuShortcut( KeyEvent.VK_L ) ); openFileItem = new MenuItem( "Open File" + ellipsis, new MenuShortcut( KeyEvent.VK_O ) ); saveAsItem = new MenuItem( "Save As" + ellipsis, new MenuShortcut( KeyEvent.VK_S ) ); uploadFileItem = new MenuItem( "Upload File" + ellipsis ); printItem = new MenuItem( "Print" + ellipsis, new MenuShortcut( KeyEvent.VK_P ) ); closeItem = new MenuItem( "Close", new MenuShortcut( KeyEvent.VK_W ) ); exitItem = new MenuItem( "Exit", new MenuShortcut( KeyEvent.VK_Q ) ); // Build the menu: add( newWebBrowserItem ); add( newMailMessageItem ); add( mailDocumentItem ); addSeparator(); add( openLocationItem ); add( openFileItem ); add( saveAsItem ); add( uploadFileItem ); addSeparator(); add( printItem ); addSeparator(); add( closeItem ); add( exitItem ); // Disable menu item(s): uploadFileItem.setEnabled( false ); // Listen for ActionEvents generated by menu items: newWebBrowserItem.addActionListener( this ); exitItem.addActionListener( this ); } // ActionEvent handler: public void actionPerformed( ActionEvent e ) { // File:New if ( e.getSource() == newWebBrowserItem ) { new NewWebBrowser(); // File:Exit } else if ( e.getSource() == exitItem ) { setVisible( false ); dispose(); if ( --num_windows == 0 ) { // System.exit(0); } } } } class EditMenu extends Menu implements ActionListener { // Menu constructor: public EditMenu() { // Invoke the constructor of the superclass: super( "Edit" ); } // ActionEvent handler: public void actionPerformed( ActionEvent e ) { } } class ViewMenu extends Menu implements ActionListener { // Menu constructor: public ViewMenu() { // Invoke the constructor of the superclass: super( "View" ); } // ActionEvent handler: public void actionPerformed( ActionEvent e ) { } } class GoMenu extends Menu implements ActionListener { // Menu constructor: public GoMenu() { // Invoke the constructor of the superclass: super( "Go" ); } // ActionEvent handler: public void actionPerformed( ActionEvent e ) { } } class BookmarksMenu extends Menu implements ActionListener { // Menu constructor: public BookmarksMenu() { // Invoke the constructor of the superclass: super( "Bookmarks" ); } // ActionEvent handler: public void actionPerformed( ActionEvent e ) { } } class OptionsMenu extends Menu implements ActionListener { // Menu item names: MenuItem generalPrefs, mailAndNewsPrefs, networkPrefs, securityPrefs; // Checkable menu item names: CheckboxMenuItem showMenubarItem, showToolbarItem, showLocationItem, showDirectoryButtonsItem, showJavaConsoleItem; // Menu constructor: public OptionsMenu() { // Invoke the constructor of the superclass: super( "Options" ); // Menu items: generalPrefs = new MenuItem( "General Preferences" + ellipsis ); mailAndNewsPrefs = new MenuItem( "Mail and News Preferences" + ellipsis ); networkPrefs = new MenuItem( "Network Preferences" + ellipsis ); securityPrefs = new MenuItem( "Security Preferences" + ellipsis ); // Checkable menu items: showMenubarItem = new CheckboxMenuItem( "Show Menubar" ); showMenubarItem.setState( true ); // checked by default showToolbarItem = new CheckboxMenuItem( "Show Toolbar" ); showLocationItem = new CheckboxMenuItem( "Show Location" ); showDirectoryButtonsItem = new CheckboxMenuItem( "Show Directory Buttons" ); showJavaConsoleItem = new CheckboxMenuItem( "Show Java Console" ); // Build the menu: add( generalPrefs ); add( mailAndNewsPrefs ); add( networkPrefs ); add( securityPrefs ); addSeparator(); add( showMenubarItem ); add( showToolbarItem ); add( showLocationItem ); add( showDirectoryButtonsItem ); add( showJavaConsoleItem ); addSeparator(); add( new DocEncodingSubmenu() ); } // ActionEvent handler: public void actionPerformed( ActionEvent e ) { } class DocEncodingSubmenu extends Menu implements ActionListener { // Menu constructor: public DocEncodingSubmenu() { // Invoke the constructor of the superclass: super( "Document Encoding" ); // Build the menu: add( new CheckboxMenuItem( "Western (Latin-1)", true ) ); addSeparator(); add( new CheckboxMenuItem( "Central European (Latin-2)" ) ); addSeparator(); add( new CheckboxMenuItem( "Japanese (Auto-Detect)" ) ); add( new CheckboxMenuItem( "Japanese (Shift-JIS)" ) ); add( new CheckboxMenuItem( "Japanese (EUC-JP)" ) ); // (many more such items omitted) } // ActionEvent handler: public void actionPerformed( ActionEvent e ) { } } } class DirectoryMenu extends Menu implements ActionListener { // Menu constructor: public DirectoryMenu() { // Invoke the constructor of the superclass: super( "Directory" ); } // ActionEvent handler: public void actionPerformed( ActionEvent e ) { } } class WindowMenu extends Menu implements ActionListener { // Menu constructor: public WindowMenu() { // Invoke the constructor of the superclass: super( "Window" ); } // ActionEvent handler: public void actionPerformed( ActionEvent e ) { } } class HelpMenu extends Menu implements ActionListener { // Menu constructor: public HelpMenu() { // Invoke the constructor of the superclass: super( "Help" ); // Build the menu: add( new MenuItem( "About Netscape" ) ); add( new MenuItem( "About Plug-ins" ) ); add( new MenuItem( "Registration Information" ) ); add( new MenuItem( "Software" ) ); addSeparator(); add( new MenuItem( "Handbook" ) ); } // ActionEvent handler: public void actionPerformed( ActionEvent e ) { } } }