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

  • SEARCH
  • Home
  • 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 6576667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:32:27+00:00 2026-05-25T15:32:27+00:00

That’s my support class import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TrafficLightPanel extends

  • 0
That's my support class 

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

public class TrafficLightPanel extends JPanel
{
  private JButton red, amber, green, change;
  private JLabel label, label1;
  private JPanel buttonPanel;

  public TrafficLightPanel ()
  {
    red = new JButton ("Red");
    amber = new JButton ("Amber");
    green = new JButton ("Green");
    change = new JButton ("Change");

    ButtonListener listener = new ButtonListener();
    red.addActionListener (listener);
    amber.addActionListener (listener);
    green.addActionListener (listener);
    change.addActionListener (listener);


    buttonPanel = new JPanel();
    buttonPanel.setPreferredSize (new Dimension(80, 390));
    buttonPanel.setBackground (Color.white);
    label = new JLabel ("Button Panel");
    buttonPanel.add (label);
    buttonPanel.add (red);
    buttonPanel.add (amber);
    buttonPanel.add (green);
    buttonPanel.add (change);
    label1 = new JLabel ("last pressed");
    buttonPanel.add (label1);

    setPreferredSize (new Dimension(200, 400));
    setBackground (Color.blue);
    add(buttonPanel);

    LightPanel panel = new LightPanel();
    add(panel);
    panel.setPreferredSize (new Dimension(80, 390));
    panel.setBackground (Color.cyan);

  }

  private class ButtonListener implements ActionListener
  {
    public void actionPerformed (ActionEvent event)
    {
      if (event.getSource() == red)
        label1.setText("Red");
      buttonPanel.setBackground(Color.red);
      if (event.getSource() == amber)
        label1.setText("Amber");
      buttonPanel.setBackground(Color.orange);
      if (event.getSource() == green)
        label1.setText("Green");
      buttonPanel.setBackground(Color.green);
      if(event.getSource() == change)
        label1.setText("Change");
      buttonPanel.setBackground(Color.white);
    }
  }

  private class LightPanel extends JPanel
  {
    public void paintComponent (Graphics page)
    {
      super.paintComponent(page);
      page.setColor(Color.red);
      page.fillOval(15, 30, 40, 40);
      page.setColor(Color.orange);
      page.fillOval(15, 90, 40, 40);
      page.setColor(Color.green);
      page.fillOval(15, 150, 40, 40);
    }
  }
}

And that's my application class

import javax.swing.JFrame;

public class TrafficLight
{
  public static void main (String[] args)
  {
    JFrame frame = new JFrame("Traffic Light");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().add(new TrafficLightPanel());

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

I wanted to ask as my program does exactly what it’s suppose to do, however i can’t seem to get the loop change label1 to red and at the same time change the buttonPanel background color any suggestions for that?

  • 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-25T15:32:27+00:00Added an answer on May 25, 2026 at 3:32 pm

    Not really checked the code , but shouldn’t

    if (event.getSource() == red)
       label1.setText("Red");
       buttonPanel.setBackground(Color.red);
    

    be

       if (event.getSource() == red)
        {
           label1.setText("Red");
           buttonPanel.setBackground(Color.red);
        }
    

    This is why it’s a good idea to always use {} , even in single-line conditionals .

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

Sidebar

Related Questions

That's it. If you want to document a function or a class, you put
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I need to read a server controls event, which will
That's a simple question: is there an API to draw a B-tree in Java?
That is, I have a list of class ' Entity ', which is an
That is, I'd like to have a tuple of values. The use case on
That's the question. Give only one reason you think why have OODB failed or
That's it. It's a dumb dumb (embarrassing!) question, but I've never used C# before,
That's basically the question, is there a right way to implement operator<< ? Reading
that one has been nagging me ever since I dabbled in web developpement; 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.