Java Reference
In-Depth Information
42
// respond to checkbox events
43
@Override
44
public void itemStateChanged(ItemEvent event)
45
{
46
Font font = null ; // stores the new Font
47
48
// determine which CheckBoxes are checked and create Font
49
if (
boldJCheckBox.isSelected() && italicJCheckBox.isSelected()
)
50
font = new Font( "Serif" , Font.BOLD + Font.ITALIC , 14 );
51
else if (
boldJCheckBox.isSelected()
)
52
font = new Font( "Serif" , Font.BOLD , 14 );
53
else if (
italicJCheckBox.isSelected()
)
54
font = new Font( "Serif" , Font.ITALIC , 14 );
55
else
56
font = new Font( "Serif" , Font.PLAIN , 14 );
57
58
textField.setFont(font);
59
}
60
}
61
} // end class CheckBoxFrame
Fig. 12.17 | JCheckBox es and item events. (Part 2 of 2.)
1
// Fig. 12.18: CheckBoxTest.java
2
// Testing CheckBoxFrame.
3
import javax.swing.JFrame;
4
5
public class CheckBoxTest
6
{
7
public static void main(String[] args)
8
{
9
CheckBoxFrame checkBoxFrame = new CheckBoxFrame();
10
checkBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
11
checkBoxFrame.setSize( 275 , 100 );
12
checkBoxFrame.setVisible( true );
13
}
14
} // end class CheckBoxTest
Fig. 12.18 | Testing CheckBoxFrame .
After the JTextField is created and initialized (Fig. 12.17, line 24), line 25 uses
method setFont (inherited by JTextField indirectly from class Component ) to set the font
of the JTextField to a new object of class Font (package java.awt ). The new Font is ini-
 
Search WWH ::




Custom Search