/* RadioB.java An applet to illustrate event handling with interactive radio buttons that control the font style of a text field */ import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; public class RadioB extends JApplet implements ItemListener { // Make most of the variables class variables, because both init // and the event handler must see them private Container contentPane = getContentPane(); private JTextField text; private Font plainFont, boldFont, italicFont, boldItalicFont; private JRadioButton plain, bold, italic, boldItalic; private ButtonGroup radioButtons = new ButtonGroup(); private JPanel myPanel = new JPanel(); // The init method is where the document is initially built public void init() { // Set the background color of the panel myPanel.setBackground(Color.cyan); // Create the fonts plainFont = new Font("Serif", Font.PLAIN, 16); boldFont = new Font("Serif", Font.BOLD, 16); italicFont = new Font("Serif", Font.ITALIC, 16); boldItalicFont = new Font("Serif", Font.BOLD + Font.ITALIC, 16); // Create the test text string, set its font, and add it to the // panel text = new JTextField("In what font style should I appear?",                              30); myPanel.add(text); text.setFont(plainFont); // Create radio buttons for the fonts and add them to the panel plain = new JRadioButton("Plain", true); bold = new JRadioButton("Bold"); italic = new JRadioButton("Italic"); boldItalic = new JRadioButton("Bold Italic"); radioButtons.add(plain); radioButtons.add(bold); radioButtons.add(italic); radioButtons.add(boldItalic); // Register the event handlers to myPanel plain.addItemListener(this); bold.addItemListener(this); italic.addItemListener(this); boldItalic.addItemListener(this); // Now add the buttons to the panel myPanel.add(plain); myPanel.add(bold); myPanel.add(italic); myPanel.add(boldItalic); // Now add the panel to the content pane for the applet contentPane.add(myPanel); } // End of init() // The event handler public void itemStateChanged (ItemEvent e) { // Determine which button is on and set the font accordingly if (plain.isSelected())     text.setFont(plainFont); else if (bold.isSelected())    text.setFont(boldFont); else if (italic.isSelected())    text.setFont(italicFont); else if (boldItalic.isSelected())    text.setFont(boldItalicFont); } // End of itemStateChanged } // End of RadioB applet