Java Reference
In-Depth Information
The following JCheckboxDemo program looks quite similar to the earlier
CheckboxDemo program. Compare the two and notice what changes were
needed since Swing is being used instead of AWT. For example, a JFrame is
used instead of a Frame, and the components are added to the content pane.
Sample output from the JCheckBoxDemo program is shown in Figure 13.5.
Compare the output to that in Figure 13.4.
import javax.swing.*;
import java.awt.*;
public class JCheckBoxDemo extends JFrame
{
private JCheckBox red, yellow, blue;
public JCheckBoxDemo(String title)
{
super(title);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
red = new JCheckBox(“Red”);
blue = new JCheckBox(“Blue”);
yellow = new JCheckBox(“Yellow”);
//add the checkboxes to the frame
Panel north = new Panel();
north.add(red);
north.add(blue);
north.add(yellow);
contentPane.add(north, BorderLayout.NORTH);
//register the event listener
MixSwingColors listener = new MixSwingColors(contentPane);
red.addItemListener(listener);
blue.addItemListener(listener);
yellow.addItemListener(listener);
}
public static void main(String [] args)
{
JFrame f = new JCheckBoxDemo(“JCheckBoxDemo”);
f.setSize(300,300);
f.setVisible(true);
}
}
Search WWH ::




Custom Search