I'm currently creating a GUI for a Chess game. It's a college assignment. Currently, We've finished the engine. So, for the board, I've created a 2D array of JButtons with the code below.
public class GUI extends JFrame implements ActionListener {
private JPanel gameBoard;
private JButton[][] board = new JButton[4][4];
gameBoard = new JPanel();
add(board);
gameBoard.setLayout(new GridLayout(7, 6));
gameBoard.setBounds(400, 30, 700, 700);
board = new JButton[4][4]
for(int i = 0;i<4;i++) {
for(int j = 0;j < 4;j++) {
board[i][j] = new JButton();
gameBoard.add(board[i][j]);
}
}
}
I am a beginner so I apologize if this code gives a you a nightmare haha, but I'd love it if someone took my implementation as it is and told me how to add an actual piece from the engine. The engine assembles the pieces on its own and creates a board of its own. I also initialized a new game in my GUI, so I can use my Engine's methods than getsPieces and etc. My question is, how do I attach an object of type Piece to the JButton? Thank you! Also I'm open for tips on how to improve my questions, thanks.