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.
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18
What did you try to do? and what errors do you get?
javaAddict
Nearly a Senior Poster
3,338 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 450
Skill Endorsements: 7
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.
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18
if you want to try this: try this.
if you have questions about how: don't revive a dead thread, start a new one.
stultuske
Industrious Poster
4,380 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
don't see why not. still no reason to keep this thread alive.
stultuske
Industrious Poster
4,380 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
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.
stultuske
Industrious Poster
4,380 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
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.
JamesCherrill
... trying to help
8,525 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30