Method for automatically creating JButtons?

I am currently new to Java, creating a program that is essentially a board game, almost identical to checkers. Unfortunately, this also means that I need a hundred JButtons for a chessboard. I know there is a way to automatically generate JButtons, I saw this. I have no idea how I would do this. Any help would be appreciated!

+4
source share
1 answer

If you want to create a board game, you should use GridLayout , which will help you position your buttons a lot with minimal effort. Something like that:

 public JPanel createBoardGame() JPanel boardGame = new JPanel(new GridLayout(numberOfRows,numberOfColumns)); for (int i=0; i<numberOfRows*numberOfColumns; i++) { boardGame.add(new JButton("")); } } 

Here is a good article that will help you significantly realize your needs.

+6
source

All Articles