1

Very new to programming. I am trying to create a simple traffic light. 'panel' needs to stay as a 'LightShow' object but eclipse suggests that I make it of type component. I'm getting this error:

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container

at java.desktop/java.awt.Container.checkNotAWindow(Container.java:512)

at java.desktop/java.awt.Container.addImpl(Container.java:1115)

at java.desktop/java.awt.Container.add(Container.java:436)

at guiPpTrafficLights.TrafficLightShow.main(TrafficLightShow.java:15)

//Traffic Light Show Class

import javax.swing.JFrame;

public class LightShow extends JFrame
{
    public static void main (String[] args)
    {       
        JFrame frame = new JFrame("Light");

        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        
        LightShow panel = new Lightshow();

        ```````````````````````````````````````````````````````````
        frame.getContentPane().add(panel); \\Error happening here.
        ```````````````````````````````````````````````````````````
        frame.pack();
        frame.setVisible(true);
    }
}
Community
  • 1
  • 1
  • I imported 'import java.awt.BorderLayout;' and written in 'frame.getContentPane().add(panel, BorderLayout.CENTER);'. I am receiveing the same error as before. – oneEyedMonster Mar 24 '19 at 19:09
  • 1
    A Window is a top-level component. It's not supposed to be added to any container. hence the exception. – JB Nizet Mar 24 '19 at 19:12
  • Possibly duplicate of https://stackoverflow.com/questions/39977948/illegalargumentexception-adding-a-window-to-a-container. Please try this. – Kavita Patil Mar 24 '19 at 19:12
  • Try to extend from `JPanel` instead – QBrute Mar 24 '19 at 21:06

0 Answers0