简体   繁体   中英

Jbuttons won't organize in a grid layout until i click on another window

I am making a chess game and I'm somewhat new at java so forgive me if this ends up being a result of my own stupidity, but I'm having a problem setting up the JFrame where the JPanel will show all the buttons bundled up in the upper left hand corner until i change focus onto another window and go back to the chess game as shown below

(Before refocus) 在此处输入图片说明

(After refocus)

在此处输入图片说明

I've done research and most similar problems have to do with buttons being added after setVisibility(true); but that doesn't seem to be a problem in my code, the relevant parts of my code(leaving out the chess logic parts) are below, thanks in advance for your help/:

This is the main class that holds the JFrame and the main method

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

This is the Chessboard Class that holds the Buttons/Pieces and the Frame

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(true)

以防万一有人想知道,我通过使用本机JButtons而不是我制作的chessSquare(继承了JButton)类解决了此问题,尽管不确定为什么,但这已经为我解决了。

When you extended Jbutton, did you implement/override getX() and getY(). If so, it might be the cause since these two methods are necessary by the Component class, which JButton inherits from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM