Hi, first post on the site and I hope I'll explain this properly:

I'm tasked with creating a TicTacToe Applet using a JButton array.

What I can't figure out is the problem I'm having with either setText or getSource (last bit of code). Can someone point out what I've done wrong?

Thanks!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JTicTacToe extends JApplet implements ActionListener
{
	int x = 0;
	int turn = -1;
	String letter = "";
	
	JButton buttons[] = new JButton[9];
	
	Container con = getContentPane();
	
	public void init()
	{
		con.setLayout(new GridLayout(3, 3));
		for(x=0; x < buttons.length; x++)
		{
			buttons[x] = new JButton("m");
			con.add(buttons[x]);
		}
		
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(turn==-1)
			{
				letter="-";
				turn=0;
			}
		else
		{
			turn++;
		
			if(turn % 2 == 0)
			{
				letter = "O";
			} 	
				else 
				{
					letter = "X";
				}
		}

		((JButton)e.getSource()).setText(letter);
		((JButton)e.getSource()).setEnabled(false);

		
		
	}

}

Solved... rookie mistake.

Forgot addActionListener

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.