Hello, I'm trying to get this code to display a tic tac toe game, but it displays as 9 slim buttons in a row instead of a 3 x 3 grid as it should. Can someone tell me why this is happening?
package games.board; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Board extends JComponent { private final Cell[][] cells; public Board (int rows, int columns, ActionListener ah) { cells = new Cell[rows][columns]; this.setLayout(new GridLayout()); for( int r = 0; r < cells.length; r++ ) { for (int c = 0; c < cells[r].length; c++) { cells[r][c] = new Cell(r,c); this.add(cells[r][c]); cells[r][c].addActionListener(ah); } } } public void setCell(Mark mark, int row, int column) throws IllegalArgumentException { if (cells[row][column].getContent() == Mark.EMPTY) { cells[row][column].setContent(mark); } else { throw new IllegalArgumentException("Player already there!"); } } public Cell getCell(int row, int column) { return cells[row][column]; } @Override public String toString() { StringBuilder str = new StringBuilder(); for (Cell[] cell : cells) { str.append("|"); for (Cell cell1 : cell) { switch (cell1.getContent()) { case NOUGHT: str.append("O"); break; case CROSS: str.append("X"); break; case YELLOW: str.append("Y"); break; case RED: str.append("R"); break; case BLUE: str.append("B"); break; case GREEN: str.append("G"); break; case MAGENTA: str.append("M"); break; case ORANGE: str.append("N"); break; default: //Empty str.append(" "); } str.append("|"); } str.append("\n"); } return str.toString(); } }package games.board; import java.awt.*; import javax.swing.*; public class Cell extends JButton { @Override public void paintComponent(Graphics g) { super.paintComponent(g); int offset = 5; Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(5)); switch(content) { case NOUGHT: g2.setColor(Color.RED); g2.drawOval(offset, offset, this.getWidth() - offset * 2, this.getHeight() - offset * 2); break; case CROSS: g2.setColor(Color.BLACK); g2.drawLine(offset, offset, this.getWidth() - offset , this.getHeight() - offset ); g2.drawLine(this.getWidth() - offset, offset , offset, this.getHeight()- offset); break; } } private Mark content; private final int row, column; public Cell(int row, int column) { this.row = row; this.column = column; content = Mark.EMPTY; } public Mark getContent() { return content; } public void setContent(Mark content) { this.content = content; } public int getRow() { return row; } public int getColumn() { return column; } }package tictactoeguigame; import java.awt.event.*; import games.board.*; import javax.swing.*; public class TicTacToeGUIGame extends JFrame { private final Board gb; private int turn; public static void main(String[] args) { SwingUtilities.invokeLater(() -> { TicTacToeGUIGame ticTacToeGUIGame; ticTacToeGUIGame = new TicTacToeGUIGame(); }); } private void takeTurn(Cell c) { Mark curMark = (turn++ % 2 == 0)?Mark.NOUGHT: Mark.CROSS; gb.setCell(curMark,c.getRow(),c.getColumn()); } private TicTacToeGUIGame() { gb = new Board(3, 3, (ActionEvent ae) -> { Cell c = (Cell) ae.getSource(); takeTurn(c); }); this.add(gb); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setTitle("TIC-TAC-TOE"); this.setSize(300, 300); this.setVisible(true); } }