menus - CSc 2700 Lecture 22 Menus 1 Introduction Including...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CSc 2700: Lecture # 22 Menus November 13, 2008 1 Introduction Including menus in a GUI requires the use of three Swing classes: JMenuBar, JMenu, and JMenuItem. Figure 1 shows a menu bar and two di±erent menus. The menus are the File menu, and the Edit menu. The menu bar is the bar or thin strip on which the two menus reside. Clicking on one of the menus reveals several menu items. For example clicking on the File Menu or the Edit menu shows the menu items displayed in Figure 1. Adding one or more menu to a GUI is a Figure 1: A Menu Bar, Two Menus, and Seven Menu Items three step process: 1. create a menu bar, 2. create each menu and add each menu to the menu bar, and 3. create menu items and add the menu items to the appropriate menu. 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Creating The Menu Bar To create a menu bar use the constructor JMenuBar(). To place a menu bar in a frame use the JFrame method setJMenuBar(JMenuBar menuBar). Within the constructor of a JFrame, you can add a menu bar to a frame using the statements: JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); Otherwise, you can add a menu bar as follows: JFrame frame = new JFrame(); JMenuBar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar); 3 Adding A Menu To The Menu Bar Here, the appropriate constructor is JMenu(String menu), where menu is the name of the menu. To add a menu to a menu bar, use the JMenuBar method JMenu addJMenu(JMenu m), which adds a menu, m, to the end of a menu bar. For example, the following segment adds two menus to the menu bar: JMenuBar menuBar = new JMenuBar(); // create a menu bar JMenu fileMenu = new JMenu("File"); // create a File menu JMenu editMenu = new JMenu("Edit"); // create an Edit menu menuBar.add(fileMenu); // add the File menu to the menu bar menuBar.add(editMenu); // add the Edit menu to the menu bar In fact, you can add any component to a menu bar with the Container method: Component add(Component c). 2
Background image of page 2
4 Adding Items to The Appropriate Menu As you might expect, the JMenuItem constructor is JMenuItem(String item) To add a menu item to a menu use the JMenu method JMenuItem add (JMenuItem menuItem) Program 1. The following code segment, embedded in an application, cre- ates the menus shown in Figure 1. //create the menu bar JMenuBar menuBar = new JMenuBar(); //create a menu ("File") JMenu fileMenu = new JMenu("File"); //Add the file menu to the menu bar menuBar.add(fileMenu); // create three menu items - "Open, " "Close, " and "Exit" JMenuItem openMenuItem = new JMenuItem("Open"); JMenuItem closeMenuItem = new JMenuItem("Close"); JMenuItem exitMenuItem = new JMenuItem("Exit"); // add the three menu items to the File menu fileMenu. addJMenuItem(openMenuItem); fileMenu. addJMenuItem(closeMenuItem); fileMenu. addJMenuItem(exitMenuItem);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/06/2009 for the course CSC 2700 taught by Professor Duncan during the Spring '08 term at LSU.

Page1 / 9

menus - CSc 2700 Lecture 22 Menus 1 Introduction Including...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online