Java Reference
In-Depth Information
131
132
// create style checkbox menu items
133
for
(
int
count =
0
; count < styleNames.length; count++)
134
{
135
136
137
138
styleItems[count].addItemListener(styleHandler);
// handler
139
}
140
141
142
143
144
// set up label to display text
145
displayJLabel =
new
JLabel(
"Sample Text"
,
SwingConstants.CENTER
);
146
displayJLabel.setForeground(colorValues[
0
]);
147
displayJLabel.setFont(
new
Font(
"Serif"
,
Font.PLAIN
,
72
));
148
149
getContentPane().setBackground(
Color.CYAN
);
// set background
150
add(displayJLabel,
BorderLayout.CENTER
);
// add displayJLabel
151
}
// end MenuFrame constructor
152
153
// inner class to handle action events from menu items
154
private
class
ItemHandler
implements
ActionListener
155
{
156
// process color and font selections
157
@Override
158
public
void
actionPerformed(ActionEvent event)
159
{
160
// process color selection
161
for
(
int
count =
0
; count < colorItems.length; count++)
162
{
163
if
(colorItems[count].isSelected())
164
{
165
displayJLabel.setForeground(colorValues[count]);
166
break
;
167
}
168
}
169
170
// process font selection
171
for
(
int
count =
0
; count < fonts.length; count++)
172
{
173
if
(event.getSource() == fonts[count])
174
{
175
displayJLabel.setFont(
176
new
Font(fonts[count].getText(), style,
72
));
177
}
178
}
179
180
repaint();
// redraw application
181
}
182
}
// end class ItemHandler
183
styleItems[count] =
new
JCheckBoxMenuItem(styleNames[count]);
// for style
fontMenu.add(styleItems[count]);
// add to font menu
formatMenu.add(fontMenu);
// add Font menu to Format menu
bar.add(formatMenu);
// add Format menu to menu bar
Fig. 22.5
|
JMenus
and mnemonics. (Part 4 of 5.)