我有一个JFrame,其中有一个主要的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);

    }
}


GamePanel有一个BorderLayout,而我有setPreferredSize(new Dimension(800,800))

公共类GamePanel扩展了JPanel实现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


在中心是我的BoardPanel,其中有一个GridLayout(3,3),因此BoardPanel有9个子MiniGame JPanels。这是我的问题:如下所示添加MiniGame可以正常工作,但是由于某些原因,它们被推向并显示在父BoardPanel和窗口之外。理想情况下,我想在不更改布局的情况下执行此操作,因为我知道它们以前已经工作过。这是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);
}


我知道所有面板都已添加和显示(我通过更改其边框厚度进行检查。此外,增加空白边框大小会显示更多MiniGame面板)。因此,与其像网格一样显示:

 1 2 3
 4 5 6
 7 8 9


它们以对角线向下显示:

 1
   2
     3
      ...


其余在父面板之外。

如果有帮助,请使用MiniGame构造函数:

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]);
    }
     }


谢谢大家!

最佳答案

向后工作,我意识到这是我曾经调用过的两个函数getX()和getY()。我猜这些是JPanels的默认函数,我已经覆盖了它们。

10-07 16:17