Declare three objects small, medium, and large of type jr


The files StyleOptions.java and StyleOptionsPanel.java are from Listings 5.22 and 5.23 of the text (Java Software Solutions 6th Edition - Lewis & Loftus - with a couple of slight changes-an instance variable fontSize is used rather than the literal 36 for font size and the variable style is an instance variable rather than local to the itemStateChanged method). The program demonstrates checkboxes and ItemListeners. In this exercise you will add a set of three radio buttons to let the user choose among three font sizes. The method of adding the radio buttons will be very similar to that in the QuoteOptionsPanel class (Listing 5.25 of the text). Before modifying the program compile and run the current version to see how it works and study the QuoteOptionsPanel example.

Do the following to add the radio buttons to the panel:

1. Declare three objects small, medium, and large of type JRadioButton.

2. Instantiate the button objects labeling them "Small Font," "Medium Font," "Large Font." Initialize the large font button to true. Set the background color of the buttons to cyan.

3. Instantiate a button group object and add the buttons to it.

4. Radio buttons produce action events so you need an ActionListener to listen for radio button clicks. The code you need to add to actionPerformed will be similar to that in the QuoteListener in Listing 5.25. In this case you need to set the fontSize variable (use 12 for small, 24 for medium, and 36 for large) in the if statement, then call the setFont method to set the font for the saying object. (Note: Instead of adding an ActionListener you could use the current ItemListener and add code to check to see if the source of the event was a radio button.)

5. In StyleOptionsPanel() add the ItemListener object to each button and add each button to the panel.

6. Compile and run the program. Note that as the font size changes the checkboxes and buttons re-arrange themselves in the panel. You will learn how to control layout later in the course.



---------------- Quote options coding (Panel Class then the driver class) ------------------
//********************************************************************
// QuoteOptionsPanel.java Author: Lewis/Loftus
//
// Demonstrates the use of radio buttons.
//********************************************************************

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class QuoteOptionsPanel extends JPanel
{
private JLabel quote;
private JRadioButton comedy, philosophy, carpentry;
private String comedyQuote, philosophyQuote, carpentryQuote;

//-----------------------------------------------------------------
// Sets up a panel with a label and a set of radio buttons
// that control its text.
//-----------------------------------------------------------------
public QuoteOptionsPanel()
{
comedyQuote = "Take my wife, please.";
philosophyQuote = "I think, therefore I am.";
carpentryQuote = "Measure twice. Cut once.";

quote = new JLabel (comedyQuote);
quote.setFont (new Font ("Helvetica", Font.BOLD, 24));

comedy = new JRadioButton ("Comedy", true);
comedy.setBackground (Color.green);
philosophy = new JRadioButton ("Philosophy");
philosophy.setBackground (Color.green);
carpentry = new JRadioButton ("Carpentry");
carpentry.setBackground (Color.green);

ButtonGroup group = new ButtonGroup();
group.add (comedy);
group.add (philosophy);
group.add (carpentry);

QuoteListener listener = new QuoteListener();
comedy.addActionListener (listener);
philosophy.addActionListener (listener);
carpentry.addActionListener (listener);

add (quote);
add (comedy);
add (philosophy);
add (carpentry);

setBackground (Color.green);
setPreferredSize (new Dimension(300, 100));
}

//*****************************************************************
// Represents the listener for all radio buttons
//*****************************************************************
private class QuoteListener implements ActionListener
{
//--------------------------------------------------------------
// Sets the text of the label depending on which radio
// button was pressed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
Object source = event.getSource();

if (source == comedy)
quote.setText (comedyQuote);
else
if (source == philosophy)
quote.setText (philosophyQuote);
else
quote.setText (carpentryQuote);
}
}
}


//********************************************************************
// QuoteOptions.java Author: Lewis/Loftus
//
// Demonstrates the use of radio buttons.
//********************************************************************

import javax.swing.JFrame;

public class QuoteOptions
{
//-----------------------------------------------------------------
// Creates and presents the program frame.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Quote Options");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

QuoteOptionsPanel panel = new QuoteOptionsPanel();
frame.getContentPane().add (panel);

frame.pack();
frame.setVisible(true);
}

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Declare three objects small, medium, and large of type jr
Reference No:- TGS0112849

Expected delivery within 24 Hours