1

I have a JFrame, with one main GamePanel:

import javax.swing.*;

public class GameFrame extends JFrame {

    public static void main(String[] args) {

        JFrame gameFrame = new JFrame( "Ultimate Tic Tac Toe");

        gameFrame.setResizable(false);
        gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gameFrame.add( new GamePanel() );
        gameFrame.pack();
        gameFrame.setVisible(true);

    }
}

The GamePanel has a BorderLayout and I have setPreferredSize(new Dimension(800,800)):

public class GamePanel extends JPanel implements ActionListener {

private BoardPanel boardPanel; //the main game, comprised of minigames
private static TrackingPanel trackingPanel; //keeps track of score, turn, and stuff
private ArrayList<MiniGame> miniGames;

public GamePanel() {

    super( new BorderLayout());
    setFocusable(true);
    setPreferredSize(new Dimension(800,800));

    //create panel that will hold the 9 mini games
    boardPanel = new BoardPanel(this); 

    //add actionListeners to each button
    miniGames = boardPanel.getMiniGames();
    for (MiniGame mini : miniGames) {
        for (SquareButton button : mini.getSquares())
            button.addActionListener(this);
    }

    add( trackingPanel, BorderLayout.NORTH);
    add( boardPanel, BorderLayout.CENTER);
}//end constructor

In the CENTER is my BoardPanel, which has a GridLayout(3,3), so the BoardPanel has 9 child MiniGame JPanels. HERE'S MY PROBLEM: Adding the MiniGames like below was working fine, but for some reason, they're being pushed around and displaying outside the parent BoardPanel and window. Ideally, I'd like to do this without changing the layouts, since I know they worked before. Here's the code for BoardPanel.

import java.awt.*;
import java.util.ArrayList;
import javax.swing.JPanel;

public class BoardPanel extends JPanel {

private ArrayList<MiniGame> miniGames;

public BoardPanel(GamePanel gp) {
     super(new GridLayout(3,3));
             setBorder(BorderFactory.createEmptyBorder(200,200,200,200)); 

     miniGames = new ArrayList<MiniGame>(9);
     for(int i = 1; i<=9; i++)
         miniGames.add(new MiniGame(gp, i));

     for(MiniGame mini : miniGames)
         add(mini);
}

I know the panels are all being added and displayed (I checked by changing their border thickness. Also, increasing the empty border size reveals more of the MiniGame panels). So, instead of display like a grid:

 1 2 3 
 4 5 6
 7 8 9

they're displaying diagonally down:

 1
   2
     3
      ...

Where the rest are outside the parent Panel.

Here's the MiniGame constructor if that helps:

public MiniGame(GamePanel gp, int num) {

    super( new GridLayout(3,3));
    setFocusable(true);

    trackingPanel = gp.getTrackingPanel();
    panelNum = num;

    if(panelNum==1)
        setBorder(BorderFactory.createMatteBorder(0,0,2,2,Color.BLACK));
    else if(panelNum==2)
        setBorder(BorderFactory.createMatteBorder(0,2,2,2,Color.BLACK));
    else if(panelNum==3)
        setBorder(BorderFactory.createMatteBorder(0,2,2,0,Color.BLACK));
    else if(panelNum==4)
        setBorder(BorderFactory.createMatteBorder(2,0,2,2,Color.BLACK));
    else if(panelNum==5)
        setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLACK));
    else if(panelNum==6)
        setBorder(BorderFactory.createMatteBorder(2,2,2,0,Color.BLACK));
    else if(panelNum==7)
        setBorder(BorderFactory.createMatteBorder(2,0,0,2,Color.BLACK));
    else if(panelNum==8)
        setBorder(BorderFactory.createMatteBorder(2,2,0,2,Color.BLACK));
    else
        setBorder(BorderFactory.createMatteBorder(2,2,0,0,Color.BLACK));

    squares = new SquareButton[9];

    //create squares and add squares to mini game
    for ( int i = 0; i < squares.length; i++ ) {
        squares[i] = new SquareButton(i);
        add(squares[i]);
    }
     }

Thanks all!

2
  • 1
    For better help sooner, post an SSCCE. Jul 8, 2013 at 18:28
  • Working backwards I realized that it was two functions I had called getX() and getY(). I guess those are default functions for JPanels and I was overriding them. Jul 8, 2013 at 19:07

2 Answers 2

1

Working backwards I realized that it was two functions I had called getX() and getY(). I guess those are default functions for JPanels and I was overriding them.

1
0

The "MiniGame constructor" shows that you call {add(squares[i]);}, but you don't show what add does in MiniGame. Since you don't show all its code, we don't know what it extends. If it is itself a UI class, then your problem is likely the layout manager active in that container, whatever it is.

1
  • Sorry, I should have included more code. MiniGame extends JPanel so I was just adding buttons to the panel. Jul 11, 2013 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.