Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3346180
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:16:23+00:00 2026-05-18T01:16:23+00:00

I am making a frame with 3 panels. 1 panel has some text on

  • 0

I am making a frame with 3 panels. 1 panel has some text on it, 1 has radio buttons to alter the font that the saying appears in and the last panel has radio buttons to alter the size of the font. I have everything done, but I am confused as to how to add the action listener for the size radio buttons. Can anyone help me with this? The code for the actionlistener is near the bottom. Thank you very much!

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

public class Layouts extends JPanel implements ActionListener
{
    int style = Font.PLAIN;
    String font = "Arial";
    private JLabel saying, overAll, top, sizeP, fontP;
    private JPanel panel1, panel2, panel3, panel4;
    private JRadioButton style1, style2, style3, style4, size1, size2, size3, size4;
    //-----------------------------------------------------------------
    //  Sets up a panel with a label and some check boxes that
    //  control the style of the label's font.
    //-----------------------------------------------------------------
    public Layouts()
    {
        style1 = new JRadioButton ("Arial", true);
        style2 = new JRadioButton ("Times New Roman", false);
        style3 = new JRadioButton ("Verdana", false);
        style4 = new JRadioButton ("Thonburi", false);

        size1 = new JRadioButton ("36", false);
        size2 = new JRadioButton ("24", false);
        size3 = new JRadioButton ("20", false);
        size4 = new JRadioButton ("16", true);

        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();

        saying = new JLabel ("Say it with style!");
        saying.setFont (new Font ("Helvetica", Font.PLAIN, 36)); // we'll need this later

        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);

        ButtonGroup group2 = new ButtonGroup();
        group.add(size1);
        group.add(size2);
        group.add(size3);
        group.add(size4);

        size1.addActionListener (this);
        size2.addActionListener (this);
        size3.addActionListener (this);
        size4.addActionListener (this);

        setBackground (Color.red);
        setPreferredSize (new Dimension(500, 100));

        setLayout(new BorderLayout());
        add(panel1, BorderLayout.NORTH);
        add(panel2, BorderLayout.EAST);
        add(panel3, BorderLayout.WEST);

        //add(panel2, East);
        //add(panel3, West);

        panel1.add(saying);
        panel1.setBackground(Color.yellow);

        panel2.setLayout(new GridLayout());
        panel2.setBackground(Color.red);
        panel2.add(style1);
        panel2.add(style2);
        panel2.add(style3);
        panel2.add(style4);

        panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
        panel3.setBackground(Color.red);
        panel3.add(size1);
        panel3.add(size2);
        panel3.add(size3);
        panel3.add(size4);
    }
    //*****************************************************************
    //  Represents the listener for both check boxes and the radio boxes.
    //*****************************************************************
    public void actionPerformed(ActionEvent e) // this is our bread and butter, it is basically what we will be changing
    {
        Object source = e.getSource();

        if (style1.isSelected())
            font = "Arial";
        else
            if (style2.isSelected())
                font = "Times New Roman";
            else
                if (style3.isSelected())
                    font = "Verdana";
                else
                    if (style4.isSelected())
                        font = "Thonburi";
        saying.setFont(new Font (font, style, 36));
    }
    public static void main (String[] args)
    {
        JFrame frame = new JFrame ("Layouts");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

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

        frame.pack();
        frame.setVisible(true);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:16:24+00:00Added an answer on May 18, 2026 at 1:16 am

    You don’t have to hard code style1-4 into the listener. You see how you got the source of the action, but you never used it? That source is the radio button object that fired this event. You can just get the label on the radio button for the value. And then just set the font according the label value of the radio button that just fired the event. Also you might want to split this off into two eventhandlers, one for each set of radio buttons. Makes it easier to read and easier to discern which set is being clicked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is beyond both making sense and my control. That being said here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.