• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 459
  • Last Modified:

Problems with GUI GridLayout and Panels in Frames

I'm practicing Java by working on programming "Connect 4" the board game. Here's some info about the structure of it. The application object Connect4 extends Frame and uses the default FlowLayout. A Gameboard extends Panel, uses a GridLayout, and is a member of Connect4. The Gameboard has a Canvas object for each grid element, which is used to paint the gamepieces.

Problem: When the frame is resized the grid elements resize themselves as well.

Q: Is it possible to make the grid a fixed size, regardless of the size of the Frame? I would like to have the elements on the grid stay a constant size if possible, rather than grow and shrink with the Frame. Any suggestions?

Problem: I wanted to add a TextArea as a status window for the players onto the "Connect 4" Frame. I instantiated a Panel and TextArea then added it to the Frame.
    // Gameboard has been added to "this" Frame already
    ta = new TextArea( "Status", 2, 30 );
    p  = new Panel();
    p.add( ta );
    // add status panel to the "Connect4" Frame
    this.add( p );

This seems to coverup the Gameboard, instead of being placed next to, or below it in the Connect 4 Frame. I expected both the Status Panel and Gameboard Panel to be displayed.

Q. How come both the Gameboard Panel and the Status Panel are NOT displayed in the Frame? The only this showing is the Status Panel. Any suggestions?

  • 2
1 Solution
> Is it possible to make the grid a fixed size, regardless of the size of ..

Take a Panel [boo], set layout to null, set Bounds of gridpanel to required size, add it to boo & add boo the Frame.

Default layout of Frame is not Flow, its Border.

This is the reason for both the Status Panel and Gameboard Panel not being displayed at same time. Either change the layout or add status panel to BorderLayout.SOUTH.

fivesigmaeventAuthor Commented:
Thanks membersh. Your advice worked perfectly.
fivesigmaeventAuthor Commented:
Hopefully I will be good enough to help answer others' questions!

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now