我正在制作一个国际象棋游戏,我对java有点新意,所以请原谅我,如果这最终是我自己愚蠢的结果,但我在设置JFrame时遇到问题,JPanel将显示所有按钮捆绑在左上角,直到我将焦点转移到另一个窗口并返回国际象棋游戏,如下所示
(重新聚焦前)
(重新聚焦后)
我做过研究,大多数类似的问题都与setVisibility(true);
之后添加的按钮有关
但这似乎不是我的代码中的问题,我的代码的相关部分(省略了国际象棋逻辑部分)在下面,提前感谢您的帮助/:
这是包含JFrame和主方法
的主类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);
答案 0 :(得分:0)
在向所有相关组件添加后,您应该调用pack()
JFrame
方法(基本上,就在.setVisible(true)
之前)
答案 1 :(得分:0)
万一有人想知道,我通过使用原生JButtons而不是我制作的chessSquare(继承了JButton)类解决了这个问题,但仍然不确定原因,但是我已经修复了它。
答案 2 :(得分:0)
扩展Jbutton时,是否实现/覆盖了getX()和getY()。如果是这样,那可能是原因,因为这两个方法对于JButton继承的Component类是必需的。