//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 );
}
}
}
Techboy52 0 Newbie Poster
Techboy52 0 Newbie Poster
VernonDozier 2,218 Posting Expert Featured Poster
Techboy52 0 Newbie Poster
Techboy52 0 Newbie Poster
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
VernonDozier 2,218 Posting Expert Featured Poster
new_2_java -3 Junior Poster
amitie.boo 0 Newbie Poster
stultuske 1,116 Posting Maven Featured Poster
amitie.boo 0 Newbie Poster
stultuske 1,116 Posting Maven Featured Poster
amitie.boo 0 Newbie Poster
stultuske 1,116 Posting Maven Featured Poster
amitie.boo 0 Newbie Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
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.