gpt4 book ai didi

java - 为什么我的子 JPanel 会在父 JPanel 中移动或错位?

转载 作者:太空宇宙 更新时间:2023-11-04 07:32:05 25 4
gpt4 key购买 nike

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

公共(public)类 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这是我的问题:添加像下面这样的迷你游戏工作正常,但由于某种原因,它们被推来推去并显示在父 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() 的两个函数。我猜这些是 JPanel 的默认函数,我正在覆盖它们。

关于java - 为什么我的子 JPanel 会在父 JPanel 中移动或错位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17533412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com