Previous Next |
MenusIf you're creating a program that uses a frame, you can jazz it up with a menu bar, a set of pull-down menus that is usually displayed below the title bar. Menus in Java are created using three components: JMenuBar, JMenu, and JMenuItem. A JMenuItem component is an item on a menu. It serves the same purpose as a button—click an item to make something happen. A JMenu container holds JMenuItem components, lines that separate menu items, and other interface components, such as check boxes. A JMenuBar container holds JMenu components, displaying their names. These three work together more closely than members of the Osmond Family. For example, you could put an Exit button on a menu so users can exit the program. This button could be put in a File menu, which is then placed in a menu bar, displaying the name File, along with other menu names below a frame's title bar. A JMenuItem component can be set up using the same constructor methods as a JButton component: Call JMenuItem(String) to create an item with the text of the item as the argument. The following statements create five menu items: JMenuItem gambler1 = new JMenuItem("Hold 'Em"); JMenuItem gambler2 = new JMenuItem("Fold 'Em"); JMenuItem gambler3 = new JMenuItem("Walk Away"); JMenuItem gambler4 = new JMenuItem("Run"); JMenuItem gambler5 = new JMenuItem("Count Your Money");
A JMenu container holds menu items. To create one, call the JMenu(String) constructor with the menu's name as the only argument. The following statement creates a menu titled Gamble: JMenu menu1 = new JMenu("Gamble"); After you have created a JMenu object, call its add(JMenuItem) method to add a menu item to it. Each new item will be placed at the end of the menu, so you should add items in order from top to bottom. You also can add a text label: Call the add(String) method with the text as an argument. If you would like to add something to a menu other than a JMenuItem or text, call a JMenu object's add(Component) method with a user interface component. The following statements create a check box and add it to a menu: JMenu view = new JMenu("View"); JCheckBox wordWrap = new JCheckBox("Word Wrap"); view.add(wordWrap); Menus also can contain lines that are used to visually group related items together. To add a line separator, call the addSeparator() method, which puts a line at the end of the menu. Using the five menu items created earlier, the following statements create a menu and fill it with those items and two separators: JMenu m1 = new JMenu("Gamble"); m1.add(gambler1); m1.add(gambler2); m1.addSeparator(); m1.add(gambler3); m1.add(gambler4); m1.addSeparator(); m1.add(gambler5); After you have created all your menus and filled them with menu items, you're ready to add them to a frame by putting them in a JMenuBar. JMenuBar, another container, holds JMenu objects and displays each of their names, usually in a gray horizontal panel directly below an app's title bar. To create a menu bar, call the JMenuBar() constructor method with no arguments. Call the bar's add(JMenu) method to add menus to the bar, beginning with the menu that should be displayed at the end of the bar. (The end is the rightmost position on a horizontal bar and the bottom position on a vertical bar.) When the menu bar contains all of the menus it needs, add it to a frame by calling the frame's setJMenuBar(JMenuBar) method. The following statement finishes off the ongoing example by creating a menu bar, adding a menu to it, and then placing the bar on a frame called kenny: JMenuBar jmb = new JMenuBar(); jmb.add(m1); kenny.setJMenuBar(jmb); Screenshot shows what this menu looks like on an otherwise empty frame. Screenshot A frame with a menu bar.You already know how to handle events generated by these menu items, because JMenuItem objects function like buttons, and can generate action events when they are selected. Other components such as check boxes work the same way they do when placed in other containers. |
Previous Next |