Java Reference
In-Depth Information
FIGURE 12.5:
Example of a check box.
12.8 Check Boxes
Next, we will examine how to add check boxes to our Notepad application. A check box
can be either checked on unchecked. In our example, we will add check boxes to selected
whether the font should be bold and whether it should be italic; see Figure 12.5.
A check box can be created by calling the constructor for the
JCheckBox
class. One
possible input to the constructor is the text that is to be displayed next to the check box.
A check box is similar to a button and an action listener can be associated with it. The
isSelected
method can be used to examine if the check box is selected. The modified
version of the code is shown next.
import
java .awt .
;
import
java .awt. event .
∗
;
∗
import
javax . swing .
∗
;
public class
Test
{
public static void
main(String [] args)
{
NotepadFrame f =
new
NotepadFrame () ;
f . setVisible(
true
);
}
}
class
NotepadFrame
extends
JFrame
{
private static int
WIDTH = 600;
private static int
HEIGHT = 600;
public
NotepadFrame ()
{