1

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.

4

1 回答 1

4

在这个范围内,我在 480 x 480 区域中绘制了一个网格。

  • 覆盖JPanel 的 PreferredSize

  • 然后调用JFrame.pack()JFrame.setVisible(true)作为最后的代码行

  • 必须阅读InitialThread

  • 如果只有一个JPanel( ) 则feJPanel filling entire JFrame中使用built_inBorderLayoutJFramemyFrame.add(infoPanel, BorderLayout.CENTER)而不是 FlowLayout

  • 不要扩展JFrame创建它Object作为局部变量

于 2012-12-16T09:22:12.610 回答