Chapter 29 Menus Liang Introduction to Java Programming

  • Slides: 14
Download presentation
 · Chapter 29 Menus · · · Liang, Introduction to Java Programming, Sixth

· Chapter 29 Menus · · · Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 1

Program Menu. Demo GUI Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson

Program Menu. Demo GUI Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 2

Objectives · To create menus (§ 29. 2). · To learn the menu components

Objectives · To create menus (§ 29. 2). · To learn the menu components JMenu. Bar, JMenu, JPopup. Menu, JMenu. Item, JCheck. Box. Menu. Item, and JRadio. Button. Menu. Item (§ 29. 2). · To create popup menus (§ 29. 3). · · Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 3

Menus F Java provides several classes—JMenu. Bar, JMenu. Item, JCheck. Box. Menu. Item, and

Menus F Java provides several classes—JMenu. Bar, JMenu. Item, JCheck. Box. Menu. Item, and JRadio. Button. Menu. Item —to implement menus in a frame. F A JFrame or JApplet can hold a menu bar to which the pull-down menus are attached. Menus consist of menu items that the user can select (or toggle on or off). Menu bars can be viewed as a structure to support menus. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 4

The JMenu. Bar Class A menu bar holds menus; the menu bar can only

The JMenu. Bar Class A menu bar holds menus; the menu bar can only be added to a frame. Following is the code to create and add a JMenu. Bar to a frame: Step 1: JFrame f = new JFrame(); f. set. Size(300, 200); f. set. Visible(true); JMenu. Bar mb = new JMenu. Bar(); f. set. JMenu. Bar(mb); //attach a menu bar to a //frame Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 5

The JMenu Class Step 2: You attach menus onto a JMenu. Bar. The following

The JMenu Class Step 2: You attach menus onto a JMenu. Bar. The following code creates two menus, File and Help, and adds JMenu file. Menu = new JMenu("File", false); JMenu help. Menu = new JMenu("Help", true); mb. add(file. Menu); mb. add(help. Menu); Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 6

The JMenu. Item Class Step 3: You add menu items on a menu. The

The JMenu. Item Class Step 3: You add menu items on a menu. The following code adds menu items and item separators in menu file. Menu: file. Menu. add(new JMenu. Item("new")); file. Menu. add(new JMenu. Item("open")); file. Menu. add. Separator(); file. Menu. add(new JMenu. Item("print")); file. Menu. add(new JMenu. Item("exit")); file. Menu. add. Separator(); Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 7

Submenus Step 3. 1: You can add submenus into menu items. The following code

Submenus Step 3. 1: You can add submenus into menu items. The following code adds the submenus “Unix, ” “NT, ” and “Win 95” into the menu item “Software. ” JMenu software. Help. Sub. Menu = new JMenu("Software"); JMenu hardware. Help. Sub. Menu = new JMenu("Hardware"); help. Menu. add(software. Help. Sub. Menu); help. Menu. add(hardware. Help. Sub. Menu); software. Help. Sub. Menu. add(new JMenu. Item("Unix")); software. Help. Sub. Menu. add(new JMenu. Item("NT")); software. Help. Sub. Menu. add(new JMenu. Item("Win 95")); Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 8

Check Box Menu Items Step 3. 2 help. Menu. add(new JCheck. Box. Menu. Item("Check

Check Box Menu Items Step 3. 2 help. Menu. add(new JCheck. Box. Menu. Item("Check it")); Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 9

Radio Button Menu Items Step 3. 3: JMenu color. Help. Sub. Menu = new

Radio Button Menu Items Step 3. 3: JMenu color. Help. Sub. Menu = new JMenu("Color"); help. Menu. add(color. Help. Sub. Menu); JRadio. Button. Menu. Item jrbmi. Blue, jrbmi. Yellow, jrbmi. Red; color. Help. Sub. Menu. add(jrbmi. Blue = new JRadio. Button. Menu. Item("Blue")); color. Help. Sub. Menu. add(jrbmi. Yellow = new JRadio. Button. Menu. Item("Yellow")); color. Help. Sub. Menu. add(jrbmi. Red = new JRadio. Button. Menu. Item("Red")); Button. Group btg = new Button. Group(); btg. add(jrbmi. Blue); btg. add(jrbmi. Yellow); btg. add(jrbmi. Red); Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 10

Radio Button Menu Items JMenu color. Help. Sub. Menu = new JMenu("Color"); help. Menu.

Radio Button Menu Items JMenu color. Help. Sub. Menu = new JMenu("Color"); help. Menu. add(color. Help. Sub. Menu); JRadio. Button. Menu. Item jrbmi. Blue, jrbmi. Yellow, jrbmi. Red; color. Help. Sub. Menu. add(jrbmi. Blue = new JRadio. Button. Menu. Item("Blue")); color. Help. Sub. Menu. add(jrbmi. Yellow = new JRadio. Button. Menu. Item("Yellow")); color. Help. Sub. Menu. add(jrbmi. Red = new JRadio. Button. Menu. Item("Red")); Button. Group btg = new Button. Group(); btg. add(jrbmi. Blue); btg. add(jrbmi. Yellow); btg. add(jrbmi. Red); Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 11

29. 2. 2 Image Icons, Keyboard Mnemonics, and Keyboard Accelerators JMenu. Item jmi. New,

29. 2. 2 Image Icons, Keyboard Mnemonics, and Keyboard Accelerators JMenu. Item jmi. New, jmi. Open; file. Menu. add(jmi. New = new JMenu. Item("New")); file. Menu. add(jmi. Open = new JMenu. Item("Open")); jmi. New. set. Icon(new Image. Icon("image/new. gif")); jmi. Open. set. Icon(new Image. Icon("image/open. gif")); help. Menu. set. Mnemonic('H'); file. Menu. set. Mnemonic('F'); jmi. New. set. Mnemonic('N'); jmi. Open. set. Mnemonic('O'); jmi. Open. set. Accelerator(Key. Stroke. get. Key. Stroke(Key. Event. VK_O, Action. Event. CTRL_MASK)); Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 12

29. 2. 3 Example: Using Menus F Objective: Create a user interface that performs

29. 2. 3 Example: Using Menus F Objective: Create a user interface that performs arithmetic. F The interface contains labels and text fields for Number 1, Number 2, and Result. F The Result box displays the result of the arithmetic operation between Number 1 and Number 2. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 13

Example: Using Menus Problem: Create a user interface that performs arithmetic. The interface contains

Example: Using Menus Problem: Create a user interface that performs arithmetic. The interface contains labels and text fields for Number 1, Number 2, and Result. The Result box displays the result of the arithmetic operation between Number 1 and Number 2. Menu. Demo Run Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 14