Java Reference
In-Depth Information
FIGURE 12.4:
A font combo box.
Sometimes, we just want to create a font object without examining the fonts that are
available in the operating system. This is why Java always supports to following five basic
fonts:
SansSerif
,
Serif
,
Monospaced
,
Dialog
,and
DialogInput
. In our example, we can
create the font combo box as follows.
JComboBox fontComboBox =
new
JComboBox(fontNames) ;
We will also create a combo box for the size.
JComboBox sizeComboBox =
new
JComboBox ( ) ;
for
(
int
i=8;i
<
=72;
i++ )
{
sizeComboBox . addItem( i ) ;
}
The
addItem
method adds the specified object to the combo box. In our example, the
integers are automatically converted to objects of type
Integer
.
Here is the rewritten version of the
NotepadFrame
class that supports a font and size
chooser.
import
java .awt .
∗
;
import
java .awt. event .
∗
;
import
javax . swing .
∗
;
public class
Notepad
{
public static void
main(String [] args)
{
NotepadFrame f =
new
NotepadFrame () ;
f . setVisible(
true
);
}
}