ashish.gd
ashish.gd

Reputation: 1768

JPanel filling entire JFrame

I am writing a simple game withing which I am using a JFrame which contains a grid and a JPanel.
Here is my pseudo code:

void MyJframeConstructor()
{
  // some basic bootstrap logic
  // calling repaint to draw grid
  repaint();

  // Grid is drawn fine.
  // Showing user a confirm dialog box on which I add below JPanel.

  if(confirmed)
  {
   // GameInfoPanel extends JPanel.
    infoPanel = new GameInfoPanel(new FlowLayout(FlowLayout.LEFT));
    infoPanel.setPreferredSize(new Dimension(400, 100));
    infoPanel.setLocation(500, 50);
    this.add(infoPanel);
    infoPanel.validate();      
  }
}

My problem here is my JFrame or window is 480 x 680.
Within this I am drawing a grid in 480 x 480 area.
Below which I want the JPanel to be located at 500,50 with dimension 400, 100.
However, when I run this code, once the user confirms with OK, the JPanel fills up the entire JFrame.
How can I keep the panel in its location and consistent in size through out the life of the app ?
Any help is highly appreciated and thanks in advance.

Upvotes: 1

Views: 2581

Answers (1)

mKorbel
mKorbel

Reputation: 109815

Within this I am drawing a grid in 480 x 480 area.

  • override PreferredSize for JPanel

  • then call JFrame.pack() and JFrame.setVisible(true) as last code lines

  • have to read InitialThread

  • if is there only one JPanel (JPanel filling entire JFrame) then to use built_in BorderLayout in JFrame f.e. myFrame.add(infoPanel, BorderLayout.CENTER) not FlowLayout

  • don't to extend JFrame create this Object as local variable

Upvotes: 4

Related Questions