1

我有一个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

在 CENTER 是我的BoardPanel,里面有一个GridLayout(3,3),所以BoardPanel有 9 个孩子 MiniGame JPanels这是我的问题:添加像下面这样的迷你游戏工作正常,但由于某种原因,它们被推到周围并显示在父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]);
    }
     }

谢谢大家!

4

2 回答 2

1

向后工作,我意识到这是我调用的两个函数 getX() 和 getY()。我猜这些是 JPanel 的默认功能,我正在覆盖它们。

于 2013-07-08T19:08:11.603 回答
0

“MiniGame 构造函数”显示您调用了 {add(squares[i]);},但没有显示 add 在 MiniGame 中的作用。由于您没有显示其所有代码,因此我们不知道它扩展了什么。如果它本身是一个 UI 类,那么您的问题很可能是该容器中活动的布局管理器,无论它是什么。

于 2013-07-08T19:12:46.343 回答