JPanel not sizing correctly?

  • Hello everyone, I'm a making a simple UI program that consists of a JFrame and 2 JPanels. For the moment I need to put one panel on top of each other

    (like this).

    alt text

    Since we are not allowed to use layout managers, I found a way to do it by modifying the Jframe's container. An sscce can be found here.

    I've done other programs with this method before and it has worked with no problems until now. What it is supposed to do is draw something like this, where the filled rectangle is the JPanel. Instead it's filling the whole frame. What can I do?

    I've tried adding the panel directly to the frame, but the same problem exists. I've tried playing around with the sizes but this also doesn't work; for some reason if I print getSize() in the draw method, it says width=300 and height=470, which is wrong as it should be 200x200. I've also tried using setPreferedSize() and pack() instead but I need the frame to be a specific size, irrespective of the panels in it.

    In case this is relevant, in the actual program both classes are independet from each other, they are not nested classes as shown in the sscce. Any help is appreciated.

Log in to reply