so I created a Traffic light that has to be exactly like this one below:
I am supposed to use the exact code from the above video, but for some reason, it does not work even though my code matches with the code above. I think it is due to the person in that video using eclipse to code whereas I used repl.it java swing to accomplish this task.
My code:
Main.java
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main{
public static void main(String[]args){
JFrame frame = new JFrame();
JPanel panel = new StopLightPanel();
frame.add(panel);
frame.setSize(250, 350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_ClOSE);
frame.setVisible(true);
}
}
StopLightPanel.java
public class StopLightPanel extends JPanel {
StopLightDrawing light = new StopLightDrawing();
public StopLightPanel() {
JButton changebutton = new JButton("Switch");
light.setPreferredSize(new Dimension(160, 260));
buttonListener l = new buttonListener();
changebutton.addActionlistener(l);
add(light);
add(changebutton);
}
public class buttonListener implements Actionlistener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
light.changeColor();
}
}
StopLightDrawing.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
public class StopLightDrawing extends JComponent {
Color go = Color.gray;
Color slow = Color.gray;
Color stop = Color.red;
String activelight = "red";
public void paintComponent(Graphics g) {
g.setColor(Color.yellow);
g.fillRect(0, 0, 150, 250);
g.setColor(Color.black);
g.drawRect(0, 0, 150, 250);
g.setColor(stop);
}
}
After running the main file I experience quite a bit of errors:
./StopLightPanel.java:22: error: reached end of file while parsing
} Λ
./StopLightPanel.java:1: error: cannot find symbol public class StopLightPanel extends JPanel{
symbol: class JPanel
./StopLightPanel.java:15: error: cannot find symbol public class buttonListener implements Actionlistener{ A
symbol: class Actionlistener location: class StopLightPanel
./StopLightPanel.java:18: error: cannot find symbol public void actionPerformed (ActionEvent e) {
symbol: class ActionEvent
location: class StopLightPanel.buttonListener
Main.java:8: error: incompatible types: StopLightPanel cannot be converted to JPanel JPanel panel = new StopLightPanel();
Λ
Main.java:12: error: cannot find symbol
I believe the issue lies in the conversion, and how I'm using the specified elements in the code, I tried to fix StopLightPanel.java
as I think the issues lies from there, but it did not help.