Tic Tac Toe personal project
Using a loop isn't inherently wrong, the problem you're anticipating though is right -- you won't have a way of comparing and referencing the buttons since they all become anonymous with your current approach.

Instead, use an array of JButton and simply use the iterator of the loop to instantiate at that location which you can later utilize as an index to reference a specific JButton.

JButton[0] board = new JButton[9];
    for (int i = 0; i &lt; board.length; i++) {
        board[i] = new JButton("");
    }


On the topic of anonymity, as of Java 8 rather than using anonymous inner classes for Action Events you can use Lambda Expressions.

So, you can replace this chunk of code:


 btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {



With:

btn.addActionListener(e -&gt; {


Of course, you close the bracket. This also has the benefit of no longer requiring you to import ActionEvent and ActionListener directly.