26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

754 Graphical User Interface Components: Part 2 Chapter 13<br />

Look-and-Feel Observation 13.19<br />

Menus normally appear left <strong>to</strong> right in the order that they are added <strong>to</strong> a JMenuBar. 13.19<br />

Lines 80–81 create menu formatMenu and set its mnemonic <strong>to</strong> r (F is not used<br />

because that is the File menu’s mnemonic).<br />

Lines 86–87 create menu colorMenu (this will be a submenu in the Format menu)<br />

and set its mnemonic <strong>to</strong> C. Line 89 creates JRadioBut<strong>to</strong>nMenuItem array colorItems<br />

that refers <strong>to</strong> the menu items in colorMenu. Line 90 creates the But<strong>to</strong>n-<br />

Group colorGroup, which ensures that only one of the menu items in the Color<br />

submenu is selected at a time. Line 91 defines an instance of inner class ItemHandler<br />

(defined at lines 183–208) that responds <strong>to</strong> selections from the Color submenu and the<br />

Font submenu (discussed shortly). The for structure at lines 94–102 creates each JRadioBut<strong>to</strong>nMenuItem<br />

in array colorItems, adds each menu item <strong>to</strong> colorMenu,<br />

adds each menu item <strong>to</strong> colorGroup and registers the ActionListener for each<br />

menu item.<br />

Line 105 uses AbstractBut<strong>to</strong>n method setSelected <strong>to</strong> select the first element<br />

in the colorItems array. Line 108 adds the colorMenu as a submenu of the format-<br />

Menu.<br />

Look-and-Feel Observation 13.20<br />

Adding a menu as a menu item in another menu au<strong>to</strong>matically makes the added menu a submenu.<br />

When the mouse is positioned over a submenu (or the submenu’s mnemonic is<br />

pressed), the submenu expands <strong>to</strong> show its menu items. 13.20<br />

Line 109 adds a separa<strong>to</strong>r line <strong>to</strong> the menu. The separa<strong>to</strong>r appears as a horizontal line<br />

in the menu.<br />

Look-and-Feel Observation 13.21<br />

Separa<strong>to</strong>rs can be added <strong>to</strong> a menu <strong>to</strong> group menu items logically. 13.21<br />

Look-and-Feel Observation 13.22<br />

Any lightweight GUI component (i.e., a component that subclasses JComponent) can be<br />

added <strong>to</strong> a JMenu or <strong>to</strong> a JMenuBar. 13.22<br />

Lines 114–132 create the Font submenu and several JRadioBut<strong>to</strong>nMenuItems<br />

and select the first element of JRadioBut<strong>to</strong>nMenuItem array fonts. Line 139 creates<br />

a JCheckBoxMenuItem array <strong>to</strong> represent the menu items for specifying bold and<br />

italic styles for the fonts. Line 140 defines an instance of inner class StyleHandler<br />

(defined at lines 211–232) <strong>to</strong> respond <strong>to</strong> the JCheckBoxMenuItem events. The for<br />

structure at lines 143–150 creates each JCheckBoxMenuItem, adds each menu item <strong>to</strong><br />

fontMenu and registers the ItemListener for each menu item. Line 153 adds font-<br />

Menu as a submenu of formatMenu. Line 156 adds the formatMenu <strong>to</strong> bar.<br />

Lines 159–163 create a JLabel for which the Format menu items control the font,<br />

font color and font style. The initial foreground color is set <strong>to</strong> the first element of array<br />

colorValues (Color.black) and the initial font is set <strong>to</strong> TimesRoman with PLAIN<br />

style and 72-point size. Line 165 sets the background color of the window’s content pane<br />

<strong>to</strong> Color.cyan, and line 166 attaches the JLabel <strong>to</strong> the CENTER of the content pane’s<br />

BorderLayout.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!