There are already 2 checkboxes to change to bold and italic. Now I want to make four radio buttons to change the font of the saying "say it with style!". Below is my code.

//********************************************************************
//  StyleOptionsPanel.java       Author: Lewis/Loftus
//
//  Demonstrates the use of check boxes.
//********************************************************************

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class StyleOptionsPanel extends JPanel
{
    private JLabel saying;
    private JCheckBox bold, italic; // have a button for style1, button for style2, button for style3
    private JRadioButton style1, style2, style3, style4;
    {
        style1 = new JRadioButton ("Arial", true);
        style2 = new JRadioButton ("Times New Roman", true);
        style3 = new JRadioButton ("Verdana", true);
        style4 = new JRadioButton ("Thonburi", true);

        //also check out quote options panel file for example of radio buttons.
        //after creating your radio buttons, you have to create a button group. copy line exactly from quote options panel
        //comedy = new JRadioButton ("Comedy", true);
        //comedy.setBackground (Color.green);
        ButtonGroup group = new ButtonGroup(); //use this code for the homework
        group.add (style1);
        group.add (style2);
        group.add (style3);
        group.add (style4);

        style1.addActionListener (this);
        style2.addActionListener (this);
        style3.addActionListener (this);
        style4.addActionListener (this);

        add (style1);
        add (style2);
        add (style3);
        add (style4);
    }
    //-----------------------------------------------------------------
    //  Sets up a panel with a label and some check boxes that
    //  control the style of the label's font.
    //-----------------------------------------------------------------
    public StyleOptionsPanel()
    {
        saying = new JLabel ("Say it with style!");
        saying.setFont (new Font ("Helvetica", Font.PLAIN, 36)); // we'll need this later

        bold = new JCheckBox ("Bold"); // what is in quotes doesn't have to match variable being created but it should be descriptive
        bold.setBackground (Color.green); // you can also set the background color of the checkbox
        italic = new JCheckBox ("Italic");
        italic.setBackground (Color.green); // this doesn't seem to change anything for me?

        StyleListener listener = new StyleListener(); //whenever you create a textbox, you have to add a listener for it
        bold.addItemListener (listener);
        italic.addItemListener (listener);

        add (saying); //it matters what order these go in, it affects the way that your program looks
        add (bold);
        add (italic);

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

    //*****************************************************************
    //  Represents the listener for both check boxes.
    //*****************************************************************
    private class StyleListener implements ItemListener
    {
        //--------------------------------------------------------------
        //  Updates the style of the label font style.
        //--------------------------------------------------------------
        public void itemStateChanged (ItemEvent event) // this is our bread and butter, it is basically what we will be changing
        {
            int style = Font.PLAIN;

            if (bold.isSelected()) //this is a boolean statement, on the inside you will always expect some kind of boolean statement
                style = Font.BOLD;

            if (italic.isSelected())
                style += Font.ITALIC; // style += means that it takes whatever style the font already is and then adds the value Italic to it
            //public void actionPerformed (ActionEvent event) //also use this code in homework

            Object source = event.getSource();

            if (source == style1) //if (source == style1)
                saying.setText (Arial); //quote.setText(Helvetica)
            else
                if (source == style2)
                    saying.setText (TimesNewRoman);
                else
                    if (source == style3)
                        saying.setText (Verdana);
                    else
                        if (source == style4)
                            saying.setText(Thonburi);
        }

    }
}

Here are my error messages from the console:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (StyleOptionsPanel)
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (StyleOptionsPanel)
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (StyleOptionsPanel)
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (StyleOptionsPanel)
Arial cannot be resolved to a variable
TimesNewRoman cannot be resolved to a variable
Verdana cannot be resolved to a variable
Thonburi cannot be resolved to a variable

at StyleOptionsPanel.<init>(StyleOptionsPanel.java:33)
at StyleOptions.main(StyleOptions.java:19)

I'm not sure why that .addActionListener bit is giving me problems. I made it the way it is above because the IDE that I'm using (Eclipse) said to do that to resolve an error.

When you add an action listener to some object (like a button), the object that you add must implement the ActionListener interface, which contains one method called actionPerformed. The way this works is when you click a button, the JVM will notify any "interested parties" of the event, meaning it will call actionPerformed on any registered listeners. The code you have above doesn't compile because the StyleOptionsPanel class doesn't implement ActionListener. To fix this, you need to do 2 things:

First change the line that declares your class to the following.

public class StyleOptionsPanel extends JPanel implements ActionListener

Then, put the following method inside your class

public void actionPerformed(ActionEvent e) {
   // put what you want to happen on the buttons here
}

Hmm, I am still stumped. I also now tried to include both actionListener and itemStateChanged in the StyleListener class.
I am not understanding why it is giving me errors in the actionPerformed section

//********************************************************************
//  StyleOptionsPanel.java       Author: Lewis/Loftus
//
//  Demonstrates the use of check boxes.
//********************************************************************

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

public class StyleOptionsPanel extends JPanel implements ActionListener, ItemListener
{
    private JLabel saying;
    private JCheckBox bold, italic; // have a button for style1, button for style2, button for style3
    private JRadioButton style1, style2, style3, style4; //creates the buttons for each of the 4 styles
    {
        bold = new JCheckBox ("Bold", true);
        italic = new JCheckBox ("Italic", true);

        style1 = new JRadioButton ("Arial", true);
        style2 = new JRadioButton ("Times New Roman", false);
        style3 = new JRadioButton ("Verdana", false);
        style4 = new JRadioButton ("Thonburi", false);

        //also check out quote options panel file for example of radio buttons.
        //after creating your radio buttons, you have to create a button group. copy line exactly from quote options panel
        //comedy = new JRadioButton ("Comedy", true); - example of assigning radio button
        //comedy.setBackground (Color.green);
        ButtonGroup group = new ButtonGroup(); //use this code for the homework
        group.add (style1);
        group.add (style2);
        group.add (style3);
        group.add (style4);

        style1.addActionListener (this);
        style2.addActionListener (this);
        style3.addActionListener (this);
        style4.addActionListener (this);

        add (style1);
        add (style2);
        add (style3);
        add (style4);
    }
    //-----------------------------------------------------------------
    //  Sets up a panel with a label and some check boxes that
    //  control the style of the label's font.
    //-----------------------------------------------------------------
    public StyleOptionsPanel()
    {
        saying = new JLabel ("Say it with style!");
        saying.setFont (new Font ("Helvetica", Font.PLAIN, 36)); // we'll need this later

        bold = new JCheckBox ("Bold"); // what is in quotes doesn't have to match variable being created but it should be descriptive
        bold.setBackground (Color.green); // you can also set the background color of the checkbox
        italic = new JCheckBox ("Italic");
        italic.setBackground (Color.green); // this doesn't seem to change anything for me?

        //StyleListener listener = new StyleListener(); //whenever you create a textbox, you have to add a listener for it
        //bold.addActionListener (listener);
        //italic.addActionListener (listener);

        add (saying); //it matters what order these go in, it affects the way that your program looks
        add (bold);
        add (italic);
        add (style1);
        add (style2);
        add (style3);
        add (style4);

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

    //*****************************************************************
    //  Represents the listener for both check boxes and the radio boxes.
    //*****************************************************************
    private class StyleListener implements ActionListener, ItemListener
    {
        //--------------------------------------------------------------
        //  Updates the style of the label font style.
        //--------------------------------------------------------------
        public void itemStateChanged(ItemEvent e) 
        {
            int style = Font.PLAIN;

            if (bold.isSelected()) //this is a boolean statement, on the inside you will always expect some kind of boolean statement
                style = Font.BOLD;

            if (italic.isSelected())
                style += Font.ITALIC; // style += means that it takes whatever style the font already is and then adds the value Italic to it
            //public void actionPerformed (ActionEvent event) //also use this code in homework

            Object source = e.getSource();// TODO Auto-generated method stub

        }

        public void actionPerformed(ActionEvent e) // this is our bread and butter, it is basically what we will be changing
        {


            if (source == style1) //if (source == style1)
                saying.setText (Arial); //quote.setText(Helvetica)
            else (source == style2)
            saying.setText (TimesNewRoman);
            else (source == style3)
            saying.setText (Verdana);
            else (source == style4)
            saying.setText(Thonburi);
        }

    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }


}

lines 92-98 the editing has got screwed up here -
Object source = e.getSource(); needs to inside ActionPerformed

You now have 2 classes (StyleOptionsPanel and StyleListener) implementing ActionListener and ItemListener. You don't need both classes to do that. You could have StyleOptionsPanel implement ActionListener and StyleListener implement ItemListener, or you could have StyleListener implement both interfaces and then StyleOptionsPanel doesn't need to implement either one. Choose one approach and stick with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.