668 Graphical User Interface Components: Part 1 Chapter (Web hosting)

668 Graphical User Interface Components: Part 1 Chapter 12 options (i.e., multiple options in the group would not be selected at the same time). The logical relationship between radio buttons is maintained by a ButtonGroupobject (package javax.swing). The ButtonGroupobject itself is not a GUI component. Therefore, a ButtonGroup object is not displayed in a user interface. Rather, the individual JRadioButtonobjects from the group are displayed in the GUI. Common Programming Error 12.5 Adding a ButtonGroup object (or an object of any other class that does not derive from Component) to a container is a syntax error. The application of Fig. 12.12 is similar to the preceding program. The user can alter the font style of a JTextField s text. The program uses radio buttons that permit only a single font style in the group to be selected at a time. 1 // Fig. 12.12: RadioButtonTest.java 2 // Creating radio buttons using ButtonGroup and JRadioButton. 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 RadioButtonTest extends JFrame { 12 private JTextField field; 13 private Font plainFont, boldFont, italicFont, boldItalicFont; 14 private JRadioButton plainButton, boldButton, italicButton, 15 boldItalicButton; 16 private ButtonGroup radioGroup; 17 18 // create GUI and fonts 19 public RadioButtonTest() 20 { 21 super( “RadioButton Test” ); 22 23 // get content pane and set its layout 24 Container container = getContentPane(); 25 container.setLayout( new FlowLayout() ); 26 27 // set up JTextField 28 field = 29 new JTextField( “Watch the font style change”, 25 ); 30 container.add( field ); 31 32 // create radio buttons 33 plainButton = new JRadioButton( “Plain”, true ); 34 container.add( plainButton ); 35 36 boldButton = new JRadioButton( “Bold”, false); 37 container.add( boldButton ); Fig. 12.12 Fig. 12.12 Creating and manipulating radio button (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply