Java Reference
In-Depth Information
78
JMenu formatMenu =
new
JMenu(
"Format"
);
// create format menu
formatMenu.setMnemonic(
'r'
);
// set mnemonic to r
79
80
81
// array listing string colors
82
String[] colors = {
"Black"
,
"Blue"
,
"Red"
,
"Green"
};
83
84
JMenu colorMenu =
new
JMenu(
"Color"
);
// create color menu
colorMenu.setMnemonic(
'C'
);
// set mnemonic to C
85
86
87
// create radio button menu items for colors
88
colorItems =
new
JRadioButtonMenuItem[colors.length];
colorButtonGroup =
new
ButtonGroup();
// manages colors
89
90
ItemHandler itemHandler =
new
ItemHandler();
// handler for colors
91
92
// create color radio button menu items
93
for
(
int
count =
0
; count < colors.length; count++)
94
{
95
96
97
98
99
colorItems[count].addActionListener(itemHandler);
100
}
101
102
103
104
105
106
107
// array listing font names
108
String[] fontNames = {
"Serif"
,
"Monospaced"
,
"SansSerif"
};
109
110
111
112
// create radio button menu items for font names
113
114
115
116
// create Font radio button menu items
117
for
(
int
count =
0
; count < fonts.length; count++)
118
{
119
120
121
122
fonts[count].addActionListener(itemHandler);
// add handler
123
}
124
125
126
127
128
String[] styleNames = {
"Bold"
,
"Italic"
};
// names of styles
129
130
StyleHandler styleHandler =
new
StyleHandler();
// style handler
Fig. 22.5
|
JMenus
and mnemonics. (Part 3 of 5.)
colorItems[count] =
new
JRadioButtonMenuItem(colors[count]);
// create item
colorMenu.add(colorItems[count]);
// add item to color menu
colorButtonGroup.add(colorItems[count]);
// add to group
colorItems[0].setSelected(
true
);
// select first Color item
formatMenu.add(colorMenu);
// add color menu to format menu
formatMenu.addSeparator();
// add separator in menu
JMenu fontMenu =
new
JMenu(
"Font")
;
// create font menu
fontMenu.setMnemonic(
'n'
);
// set mnemonic to n
fonts =
new
JRadioButtonMenuItem[fontNames.length];
fontButtonGroup =
new
ButtonGroup();
// manages font names
fonts[count] =
new
JRadioButtonMenuItem(fontNames[count]);
fontMenu.add(fonts[count]);
// add font to font menu
fontButtonGroup.add(fonts[count]);
// add to button group
fonts[
0
].setSelected(
true
);
// select first Font menu item
fontMenu.addSeparator();
// add separator bar to font menu
styleItems =
new
JCheckBoxMenuItem[styleNames.length];