Here's the code:

class SingleBoard extends JPanel
    {
        public SingleBoard (ActionListener e)
        {
            setLayout (new GridLayout (3, 3));
            for (int i = 0 ; i <= 8 ; i++)
            {
                buttons [i] = new JButton ();
                buttons [i].addActionListener (e);
                add (buttons [i]);
            }

        }
    }


    public void init ()
    {
        setSize (300, 700);

        JLabel message = new JLabel ("X will go first");
        getContentPane ().setLayout (new BorderLayout ());
        getContentPane ().add (message, BorderLayout.NORTH);

        getContentPane ().setLayout (new GridLayout (5, 5, 10, 10));
        for (int i = 0 ; i < 3 ; i++)
        {
            SingleBoard sb = new SingleBoard (this);
            getContentPane ().add (sb);
        }
    }

My only problem is that the last panel is the only one I think retains the variable names buttons[0]-buttons[8], so I'm having problem to check the winner. I don't know how to access/reference the buttons on the first 2 panels

Where is the buttons array defined? You might make a getter method to access it from outside the class (as the references it stores should still be valid).

here is the whole code:

// The "TicTacToe3d" class.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import hsa.*;

public class TicTacToe3D extends JApplet implements ActionListener
{
    //No. of panels (3 boards)
    public static final int panels = 3;

    private JButton[] buttons = new JButton [27];


    private boolean checkWin = false;


    //Count turns starts at 0
    int count = 0;

    //X or O variable
    String symbol = "";

    //Sub - class for a single board
    class SingleBoard extends JPanel
    {
        int i;
        public SingleBoard (ActionListener e)
        {
            setLayout (new GridLayout (3, 3));
            for (i = 0 ; i <= 8 ; i++)
            {
                buttons [i] = new JButton ();
                buttons [i].addActionListener (e);
                add (buttons [i]);
            }

        }

    }

    public JButton[] getButtons ()
    {
        return buttons;
    }


    public void init ()
    {
        //Set the applet size for better viewing
        setSize (300, 700);

        //Message for the players
        JLabel message = new JLabel ("X will go first");
        getContentPane ().setLayout (new BorderLayout ());
        getContentPane ().add (message, BorderLayout.NORTH);

        //Adding 3 boards
        getContentPane ().setLayout (new GridLayout (5, 5, 10, 10));

        //Adding 3 boards
        getContentPane ().setLayout (new GridLayout (5, 5, 10, 10));
        for (int i = 0 ; i < 3 ; i++)
        {
            SingleBoard sb = new SingleBoard (this);
            getContentPane ().add (sb);
        }

    }



    public void actionPerformed (ActionEvent click)
    {

        count++;

        //If the count is an even number (divisible by 2) then it sets the symbol as O
        if (count % 2 == 0)
        {
            symbol = "O";
        }
        else
        {
            symbol = "X";
        }

        //If a button is click it either puts on an X or O depending on count then disable clicking it again
        JButton pressedButton = (JButton) click.getSource ();
        pressedButton.setText (symbol);
        pressedButton.setFont (new Font ("Comic Sans MS", Font.BOLD, 40));
        pressedButton.setEnabled (false);

        if (buttons [0].getText () == buttons [1].getText () && buttons [1].getText () == buttons [2].getText () && buttons [0].getText () != "")
        {
            checkWin = true;
        }
        if (checkWin == true)
        {
            JOptionPane.showMessageDialog (null, symbol + " wins the game!");
            System.exit (0);
        }

    }
} // TicTacToe3d class

Right now I only have the first line of the boards to be checked... and it is only working the last panel

if (buttons [0].getText () == buttons [1].getText () && buttons [1].getText () == buttons [2].getText () && buttons [0].getText () != "")
        {
            checkWin = true;
        }

Does this method sets like a reference to each of the JButtons that are added?

public JButton[] getButtons ()
{
return buttons;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.