Java Reference
In-Depth Information
184
// inner class to handle item events from checkbox menu items
185
private
class
StyleHandler
implements
ItemListener
186
{
187
// process font style selections
188
@Override
189
public
void
itemStateChanged(ItemEvent e)
190
{
191
String name = displayJLabel.getFont().getName();
// current Font
192
Font font;
// new font based on user selections
193
194
// determine which items are checked and create Font
195
if
(styleItems[
0
].isSelected() &&
196
styleItems[
1
].isSelected())
197
font =
new
Font(name,
Font.BOLD
+
Font.ITALIC
,
72
);
198
else if
(styleItems[
0
].isSelected())
199
font =
new
Font(name,
Font.BOLD
,
72
);
200
else if
(styleItems[
1
].isSelected())
201
font =
new
Font(name,
Font.ITALIC
,
72
);
202
else
203
font =
new
Font(name,
Font.PLAIN
,
72
);
204
205
displayJLabel.setFont(font);
206
repaint();
// redraw application
207
}
208
}
209
}
// end class MenuFrame
Fig. 22.5
|
JMenus
and mnemonics. (Part 5 of 5.)
1
// Fig. 22.6: MenuTest.java
2
// Testing MenuFrame.
3
import
javax.swing.JFrame;
4
5
public
class
MenuTest
6
{
7
public
static
void
main(String[] args)
8
{
9
MenuFrame menuFrame =
new
MenuFrame();
10
menuFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);
11
menuFrame.setSize(
500
,
200
);
12
menuFrame.setVisible(
true
);
13
}
14
}
// end class MenuTest
Menu
Mnemonic
characters
Menu bar
Fig. 22.6
|
Test class for
MenuFrame
. (Part 1 of 2.)