1
//Hey I need some help
//Write a JAVA application that displays a traffic light (three circles inside a rectangle with //red, yellow, and green color) and three buttons with red, yellow and green titles. The //application should turn the appropriate light on when the appropriate button is clicked. Two //lights can not be on at the same time. 
//This program compiles but it does not implement my action listner buttons can anyone help!!!


import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

 public class TrafficLight extends JPanel
 {
    public TrafficLight()
    {
    JButton btn1 = new JButton("Green");
    JButton btn2 = new JButton("Yellow");
    JButton btn3 = new JButton("Red");
    btn1.addActionListener(new ButtonListener());
    add(btn1);
    btn2.addActionListener(new ButtonListener());
    add(btn2);
    btn2.addActionListener(new ButtonListener());
    add(btn2);

    }

 public static void main(String[] args) 
 {
    JFrame f = new JFrame("Traffic Light");
    JPanel lights = new JPanel( new GridLayout(0,3) );
    lights.add( new TrafficSignal(Color.green) );
    lights.add( new TrafficSignal(Color.yellow) );
    lights.add( new TrafficSignal(Color.red) );
 // lights.add(new JButton("Green"));
 // lights.add(new JButton("Yellow"));
  //  lights.add(new JButton("Red"));


    f.setContentPane( lights );
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
 }
}

class ButtonListener implements ActionListener 
{
  ButtonListener() 
  {
  }

  public void actionPerformed(ActionEvent e)
         {
        if (e.getActionCommand().equals("Green")) 
            {
            btn1.setEnabled(true);
            btn2.setEnabled(false);
            btn3.setEnabled(false);
        } 
        if (e.getActionCommand().equals("Yellow"))
        {
            btn2.setEnabled(true);
            btn1.setEnabled(false);
            btn3.setEnabled(false);
        }
            else 
        {
            btn3.setEnabled(true);
            btn1.setEnabled(false);
            btn2.setEnabled(false);
        }
    }
  class TrafficSignal extends JPanel


 {

        Color on;
        int radius = 75;
        int border = 10;
        boolean active;




 TrafficSignal(Color color) 
 {
    on = color;
    active = true;
 }

 public Dimension getPreferredSize() 
 {
    int size = (radius+border)*2;
    return new Dimension( size, size );
 }

 public void paintComponent(Graphics g) 
 {
  g.setColor( Color.black );
    g.fillRect(0,0,getWidth(),getHeight());

 if (active) 
 {
 g.setColor( on );
 } 
 else 
 {
 g.setColor( on.darker().darker().darker() );
 }
 g.fillOval( border,border,2*radius,2*radius );
 }
 }
}

Edited by JamesCherrill: Closing this zombie

Votes + Comments
http://ssendy-java.blogspot.com/2012/09/traffic-lights-java-program.html check this one
7
Contributors
15
Replies
21
Views
9 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • Try changing this: [code=JAVA] import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TrafficLight extends JPanel { public TrafficLight() { JButton btn1 = new JButton("Green"); JButton btn2 = new JButton("Yellow"); JButton btn3 = new JButton("Red"); btn1.addActionListener(new ButtonListener()); add(btn1); btn2.addActionListener(new ButtonListener()); add(btn2); btn2.addActionListener(new ButtonListener()); add(btn2); } [/code] to … Read More

0

It compiles just need the buttons to turn the red,green, and yellow light on and off

0

It compiles just need the buttons to turn the red,green, and yellow light on and off

This doesn't compile for me.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TrafficLight extends JPanel {
    public TrafficLight() {
        JButton btn1 = new JButton("Green");
        JButton btn2 = new JButton("Yellow");
        JButton btn3 = new JButton("Red");
        btn1.addActionListener(new ButtonListener());
        add(btn1);
        btn2.addActionListener(new ButtonListener());
        add(btn2);
        btn2.addActionListener(new ButtonListener());
        add(btn2);
        
    }
    
    public static void main(String[] args) {
        JFrame f = new JFrame("Traffic Light");
        JPanel lights = new JPanel( new GridLayout(0,3) );
        lights.add( new TrafficSignal(Color.green) );
        lights.add( new TrafficSignal(Color.yellow) );
        lights.add( new TrafficSignal(Color.red) );
// lights.add(new JButton("Green"));
// lights.add(new JButton("Yellow"));
// lights.add(new JButton("Red"));
        
        
        f.setContentPane( lights );
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

class ButtonListener implements ActionListener {
    ButtonListener() {
    }
    
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Green")) {
            btn1.setEnabled(true);
            btn2.setEnabled(false);
            btn3.setEnabled(false);
        }
        if (e.getActionCommand().equals("Yellow")) {
            btn2.setEnabled(true);
            btn1.setEnabled(false);
            btn3.setEnabled(false);
        } else {
            btn3.setEnabled(true);
            btn1.setEnabled(false);
            btn2.setEnabled(false);
        }
    }
    class TrafficSignal extends JPanel
            
            
    {
        
        Color on;
        int radius = 75;
        int border = 10;
        boolean active;
        
        
        
        
        TrafficSignal(Color color) {
            on = color;
            active = true;
        }
        
        public Dimension getPreferredSize() {
            int size = (radius+border)*2;
            return new Dimension( size, size );
        }
        
        public void paintComponent(Graphics g) {
            g.setColor( Color.black );
            g.fillRect(0,0,getWidth(),getHeight());
            
            if (active) {
                g.setColor( on );
            } else {
                g.setColor( on.darker().darker().darker() );
            }
            g.fillOval( border,border,2*radius,2*radius );
        }
    }
}

The compiler can't find the buttons in actionPerformed in lines 45 - 58. I'm assuming those are the buttons that are defined in your constructor in lines 9 - 11. They go out of scope as soon as the constructor has been implemented.

0

well I was wondering is there a way to fix it to get the buttons to turn the lights on and off. When I try to move the code within the scope i get some weird errors.
Need help

1

Try changing this:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TrafficLight extends JPanel {
    public TrafficLight() {
        JButton btn1 = new JButton("Green");
        JButton btn2 = new JButton("Yellow");
        JButton btn3 = new JButton("Red");
        btn1.addActionListener(new ButtonListener());
        add(btn1);
        btn2.addActionListener(new ButtonListener());
        add(btn2);
        btn2.addActionListener(new ButtonListener());
        add(btn2);
        
    }

to this:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TrafficLight extends JPanel {

    JButton btn1;
    JButton btn2;
    JButton btn3;

    public TrafficLight() {
        btn1 = new JButton("Green");
        btn2 = new JButton("Yellow");
        btn3 = new JButton("Red");
        btn1.addActionListener(new ButtonListener());
        add(btn1);
        btn2.addActionListener(new ButtonListener());
        add(btn2);
        btn2.addActionListener(new ButtonListener());
        add(btn2);
        
    }

You are now declaring btn1, btn2, and btn3 outside of the constructor in a way that they'll be available for the whole class to use. See if that helps the scoping problem.

1

Try this: I changed alittle bit your program, but this should work

import java.awt.*;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TrafficLight extends JFrame implements ActionListener {
    private static JButton btn1 = null;
    private static JButton btn2 = null;
    private static JButton btn3 = null;
    
    private static TrafficSignal green = new TrafficSignal(Color.green);
    private static TrafficSignal yellow = new TrafficSignal(Color.yellow);
    private static TrafficSignal red = new TrafficSignal(Color.red);
    
    public TrafficLight(){
        super("Traffic Light");
        getContentPane().setLayout(new GridLayout(2, 1));
        btn1 = new JButton("Green");
        btn2 = new JButton("Yellow");
        btn3 = new JButton("Red");
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);        
        
        green.turnOn(true);
        yellow.turnOn(false);
        red.turnOn(false);
        
        JPanel lights = new JPanel( new FlowLayout() );
        lights.add( green );
        lights.add( yellow );
        lights.add( red );
        JPanel btnPane = new JPanel(new FlowLayout());
        btnPane.add(btn1);
        btnPane.add(btn2);
        btnPane.add(btn3);
        
        getContentPane().add(lights);
        getContentPane().add(btnPane);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);            
    }
    
    public static void main(String[] args){
        TrafficLight tl = new TrafficLight();        
        tl.setVisible(true);
    }

    
    public void actionPerformed(ActionEvent e){        
        if (e.getSource() == btn1){
            green.turnOn(true);            
            yellow.turnOn(false);
            red.turnOn(false);
        } else if (e.getSource() == btn2){
            yellow.turnOn(true);            
            green.turnOn(false);
            red.turnOn(false);
        } else if (e.getSource() == btn3){
            red.turnOn(true);            
            yellow.turnOn(false);
            green.turnOn(false);
        }
    }
}    
    
class TrafficSignal extends JPanel {
    
    Color on;
    int radius = 75;
    int border = 10;
    boolean active;
    
    TrafficSignal(Color color){
        on = color;
        active = true;
    }
    
    public void turnOn(boolean a) {
        active = a;
        repaint();        
    }
    
    public Dimension getPreferredSize(){
        int size = (radius+border)*2;
        return new Dimension( size, size );
    }
    
    public void paintComponent(Graphics g){
        g.setColor( Color.black );
        g.fillRect(0,0,getWidth(),getHeight());
    
        if (active){
            g.setColor( on );
        } else {
            g.setColor( on.darker().darker().darker() );
        }
        g.fillOval( border,border,2*radius,2*radius );
    }
}
0

if you want to try this: try this.
if you have questions about how: don't revive a dead thread, start a new one.

0

this is compiling !!! and can u tell me what shud be written in the method call after creating an object?

0

no, I can not. because I don't really know what code you are running, or what it is you are trying to do.
but, again: for new questions -> create a new thread.

0

the above source code is compiling but i dono what has to be done so that i can see the output of traffic lights!! can u plzz tell me ?i hav to submit this project on traffic lights to my teacher

0

amitie.boo
If this is a project that have to submit to your teacher then you can't just copy code from this web site. That's called cheating. We don't help peaple cheat here.

If you need any help with code that you have written yourself please start anther thread. This thread is closed.

Edited by JamesCherrill