I'm currently trying to make a basic Java Chess game where a ChessBoard is initialized and populated with ChessSquares. Every time I set the window to visible each JButton does not show up unless hovered over. How do I stop this?
ChessBoard
public class ChessBoard extends JFrame implements ActionListener {
private JPanel p = new JPanel();
private ChessSquare[][] board = new ChessSquare[8][8];
public ChessBoard(){
//Specify frame window.
setSize(640,640);
setTitle("Chess");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add panel to frame window.
p.setLayout(new GridLayout(8,8));
setContentPane(p);
//Populate Chess Board with squares.
for(int y=0; y<8; y++){
for(int x=0; x<8; x++){
board[x][y] = new ChessSquare(x,y);
board[x][y].addActionListener(this);
p.add(board[x][y]);
}
}
//Show window.
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
ChessSquare s = (ChessSquare) e.getSource();
//...
}
}
ChessSquare
import javax.swing.*;
public class ChessSquare extends JButton{
private int x, y;
public ChessSquare(int x, int y){
this.x = x;
this.y = y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}