java - 在我单击另一个窗口之前,Jbuttons 不会以网格布局进行组织

标签 java swing jpanel jbutton grid-layout

我正在制作一个国际象棋游戏,我对 java 有点陌生,所以如果这最终是我自己愚蠢的结果,请原谅我,但我在设置 JFrame 时遇到了问题,其中 JPanel 将显示所有按钮捆绑在左上角,直到我将焦点转移到另一个窗口并返回到国际象棋游戏,如下所示

(重新聚焦之前) enter image description here

(重新聚焦后)

enter image description here

我已经做过研究,大多数类似的问题都与 setVisibility(true); 之后添加的按钮有关。 但这在我的代码中似乎不是问题,我的代码的相关部分(省略国际象棋逻辑部分)如下,提前感谢您的帮助/:

这是保存 JFrame 和 main 方法的主类

public class Chess extends JFrame
{   
    ChessSquare fromSquare;
    ChessBoard board;
    JPanel panel;
    public Chess(String title)
{
    super(title);
    initialize();
    setVisible(true);
}
public void initialize()
{
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(500, 500);
    board = new ChessBoard();
    add(board.getPanel());

}
private static final long serialVersionUID = -5185475584729272657L;
public static void main(String args[] ) 
{

    Runnable r = new Runnable() {

        @Override
        public void run() {
            Chess chess = new Chess("Chess");
            chess.setVisible(true); 
        }
    };
    SwingUtilities.invokeLater(r);
}

这是保存按钮/棋子和框架的棋盘类

public class ChessBoard
{
JPanel panel;

public ChessPiece fromSquare;
public int fromRank;
public int fromFile;

public ChessPiece toSquare;
public int toRank;
public int toFile;

public ChessPiece enteredSquare;
public int enteredRank;
public int enteredFile;
public Color enteredColor;

boolean makeGray = true;
boolean colorChanged = false;

public ChessPiece [][] squares;
public Queen [] queen;
public King [] king;
public Bishop [][] bishop;
public Knight [][] knight;
public Rook [][] rook;
public Pawn [][] pawn;

public boolean pressed;

public ChessBoard()
{
    panel = new JPanel(new GridLayout(8, 8));
    setPieces();
    setInitColors();
    setIcons();
    for(int i = 0; i<8;i++)
        for(int c = 0; c<8;c++)
        {
            System.out.println(i+", "+c);
            panel.add(squares[i][c]);
        }
    //setMouseActions();
}

}
public JPanel getPanel()
{
    return panel;
}
public void setInitColors()
{
    Insets buttonMargin = new Insets(0,0,0,0);

    boolean flip = true;
    for(int i = 0; i < 8; i++)
        for(int j = 0; j < 8; j++)
    {

        if (flip)
        {
            System.out.println("i: "+i+" c: "+j+squares[i][j]+" WHITE");
            squares[i][j].setMargin(buttonMargin);
            squares[i][j].setOpaque(true);
            squares[i][j].setBorderPainted(false);
            squares[i][j].setBackground(Color.WHITE);
        }    
        else
        {
            System.out.println("i: "+i+" c: "+j+squares[i][j]+" BLACK");
            squares[i][j].setMargin(buttonMargin);
            squares[i][j].setOpaque(true);
            squares[i][j].setBorderPainted(false);
            squares[i][j].setBackground(Color.BLACK);
        }
        System.out.println(flip);
        flip = !flip;
        if (j == 7)
            flip = !flip;
        if ((i > 1) && (i < 6))
            squares[i][j].setOpaque(true);
    }
}
public void setPieces()
{
    squares = new ChessPiece [8][8];
    ImageIcon icon = new ImageIcon(
            new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB));
    for(int i = 0; i<8;i++)
        for(int c = 0; c<8;c++)
        {
            squares[i][c] = new ChessPiece(i,c,"empty");
            squares[i][c].setIcon(icon);
        }
    queen = new Queen [2];
    king = new King[2];
    bishop = new Bishop [2][2];
    knight = new Knight [2][2];
    rook = new Rook [2][2];
    pawn = new Pawn [2][8];

    queen [0] = new Queen(true);
    setSquare(queen[0],"blackqueen");
    //====================================      
    queen [1] = new Queen(false);
    setSquare(queen[1],"whitequeen");
    //====================================          
    king [0] = new King(true);
    setSquare(king [0],"blackking");
    //====================================  
    king [1] = new King(false);
    setSquare(king [1],"whiteking");
    //====================================  
    bishop [0][0] = new Bishop(true,0);
    setSquare(bishop[0][0],"blackbishop");
    //====================================  
    bishop [0][3] = new Bishop(true,1);
    setSquare(bishop[0][4],"blackbishop");
    //====================================  
    bishop [1][0] = new Bishop(false,0);
    setSquare(bishop[1][0],"whitebishop");
    //====================================  
    bishop [1][5] = new Bishop(false,1);
    setSquare(bishop[1][6],"whitebishop");
    //====================================  
    knight [0][0] = new Knight(true,0);
    setSquare(knight[0][0],"blackknight");
    //====================================  
    knight [0][7] = new Knight(true,1);
    setSquare(knight[0][8],"blackknight");
    //====================================  
    knight [1][0] = new Knight(false,0);
    setSquare(knight[1][0],"whiteknight");
    //====================================  
    knight [1][9] = new Knight(false,1);
    setSquare(knight[1][10],"whiteknight");
    //====================================  
    rook [0][0] = new Rook(true,0);
    setSquare(rook[0][0],"blackrook");
    //====================================  
    rook [0][11] = new Rook(true,1);
    setSquare(rook[0][12],"blackrook");
    //====================================  
    rook [1][0] = new Rook(false,0);
    setSquare(rook[1][0],"whiterook");
    //====================================  
    rook [1][13] = new Rook(false,1);
    setSquare(rook[1][14],"whiterook");
    //====================================  
    pawn [0][0] = new Pawn(true,1);
    setSquare(pawn[0][0],"blackpawn");
    //====================================
    pawn [0][15] = new Pawn(true,2);
    setSquare(pawn[0][16],"blackpawn");
    //====================================  
    pawn [0][2] = new Pawn(true,3);
    setSquare(pawn[0][2],"blackpawn");
    //====================================  
    pawn [0][3] = new Pawn(true,4);
    setSquare(pawn[0][3],"blackpawn");
    //====================================  
    pawn [0][4] = new Pawn(true,5);
    setSquare(pawn[0][4],"blackpawn");
    //====================================  
    pawn [0][5] = new Pawn(true,6);
    setSquare(pawn[0][5],"blackpawn");
    //====================================  
    pawn [0][6] = new Pawn(true,7);
    setSquare(pawn[0][6],"blackpawn");
    //====================================  
    pawn [0][7] = new Pawn(true,8);
    setSquare(pawn[0][7],"blackpawn");
    //====================================  
    pawn [1][0] = new Pawn(false,1);
    setSquare(pawn[1][0],"whitepawn");
    //====================================  
    pawn [1][17] = new Pawn(false,2);
    setSquare(pawn[1][18],"whitepawn");
    //====================================  
    pawn [1][2] = new Pawn(false,3);
    setSquare(pawn[1][2],"whitepawn");
    //====================================  
    pawn [1][3] = new Pawn(false,4);
    setSquare(pawn[1][3],"whitepawn");
    //====================================  
    pawn [1][4] = new Pawn(false,5);
    setSquare(pawn[1][4],"whitepawn");
    //====================================  
    pawn [1][5] = new Pawn(false,6);
    setSquare(pawn[1][5],"whitepawn");
    //====================================  
    pawn [1][6] = new Pawn(false,7);
    setSquare(pawn[1][6],"whitepawn");
    //====================================  
    pawn [1][7] = new Pawn(false,8);
    setSquare(pawn[1][7],"whitepawn");
    //====================================
}
public void setSquare(ChessPiece piece, String type)
{
    squares[piece.getX()][piece.getY()] = piece;
    System.out.println("Setting "+type+" as squares["+piece.getX()+"]["+piece.getY()+"]");
    piece.setName(type);

最佳答案

在向 JFrame 添加所有相关组件之后,您应该调用 JFramepack() 方法(基本上,就在执行 .setVisible 之前) (正确))

关于java - 在我单击另一个窗口之前,Jbuttons 不会以网格布局进行组织,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25075252/

相关文章:

java - 这些子集的排列顺序是什么?

java - 使用 REST API 从 Web 服务接收特定数据

java - 滚动 JPanel 中的可见内容

java - JPanel 不重绘

java - 独立的 JPanel

java - 尝试在java中返回HashMap值时出现空指针异常

java - Joda-Time,没有日期的时间

java,用特定符号解析xml

java - GlazedLists 清除在排序的 JTable 上抛出错误

Java小程序: actionlistener of button with noname