Web design company - Chapter 12 Graphical User Interface Components: Part 1
Chapter 12 Graphical User Interface Components: Part 1 665 taining the label for the button that was pressed by the user. ActionEvent method getActionCommandreturns the label on the button that generated the event. 12.7 JCheckBoxand JRadioButton The Swing GUI components contain three types of state buttons JToggleButton, JCheckBox and JRadioButton that have on/off or true/false values. JToggle- Buttons are frequently used with toolbars (sets of small buttons typically located on a bar across the top of a window) and are covered in Chapter 13. Classes JCheckBoxand JRadioButton are subclasses of JToggleButton. A JRadioButton is different from a JCheckBox in that there are normally several JRadioButtons that are grouped together and only one of the JRadioButtons in the group can be selected (true) at any time. We first discuss class JCheckBox. Look-and-Feel Observation 12.6 Because class AbstractButtonsupports displaying text and images on a button, all subclasses of AbstractButtonalso support displaying text and images. The application of Fig. 12.11 uses two JCheckBox objects to change the font style of the text displayed in a JTextField. One JCheckBox applies a bold style when selected and the other applies an italic style when selected. If both are selected, the style of the font is bold and italic. When the program initially executes, neither JCheckBox is checked (true). 1 // Fig. 12.11: CheckBoxTest.java 2 // Creating Checkbox buttons. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class CheckBoxTest extends JFrame { 12 private JTextField field; 13 private JCheckBox bold, italic; 14 15 // set up GUI 16 public CheckBoxTest() 17 { 18 super( “JCheckBox Test” ); 19 20 // get content pane and set its layout 21 Container container = getContentPane(); 22 container.setLayout( new FlowLayout() ); 23 24 // set up JTextField and set its font 25 field = 26 new JTextField( “Watch the font style change”, 20 ); Fig. 12.11 Program that creates two JCheckBoxbuttons (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01