Java Reference
In-Depth Information
LISTING 5.15
continued
italic =
new
JCheckBox ("Italic");
italic.setBackground (Color.cyan);
StyleListener listener =
new
StyleListener();
bold.addItemListener (listener);
italic.addItemListener (listener);
add (saying);
add (bold);
add (italic);
setBackground (Color.cyan);
setPreferredSize (
new
Dimension(300, 100));
}
//*****************************************************************
// Represents the listener for both check boxes.
//*****************************************************************
private class
StyleListener
implements
ItemListener
{
//--------------------------------------------------------------
// Updates the style of the label font style.
//--------------------------------------------------------------
public void
itemStateChanged (ItemEvent event)
{
int
style = Font.PLAIN;
if
(bold.isSelected())
style = Font.BOLD;
if
(italic.isSelected())
style += Font.ITALIC;
saying.setFont (
new
Font ("Helvetica", style, 36));
}
}
}
This program also uses the
Font
class, which represents a particular
character
font
. A
Font
object is defined by the font name, the font style, and the font size.
The font name establishes the general visual characteristics of the characters. We
are using the Helvetica font in this program. The style of a Java font can be plain,
Search WWH ::
Custom Search