Tic-Tac-Toe Project
Hey, im making tic tac toe as my final project for Programming 1, and im gonna need some help. this is what i have so far:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
publicclass TicTacToeextends JFrameimplements ActionListener
{
private JButton[][]buttons;
public JButton[][] fillArray()
{
String names="012345678";
JButton [][]buttonArray=new JButton[3][3];
int x=0;
for(int r=0;r<3;r++)
{
for(int c=0;c<3;c++)
{
buttonArray[r][c]=new JButton();
buttonArray[r][c].addActionListener(this);
buttonArray[r][c].setActionCommand( names.substring(x,x+1));
buttonArray[r][c].setBackground(Color.black);
buttonArray[r][c].setForeground(Color.white);
Font fontType =new Font("Serif", Font.BOLD, 100);
buttonArray[r][c].setFont(fontType);
contentPane.add(buttonArray[r][c]);
x++;
}
}
return buttonArray;
}
privatestatic Container contentPane;
public TicTacToe()
{
setSize(800, 600);
setTitle("Tic Tac Toe");
contentPane = getContentPane();
contentPane.setLayout(new GridLayout(3,3));
fillArray();
}
publicvoid actionPerformed(ActionEvent event)
{
Container contentPane = getContentPane();
if(event.getActionCommand().equals("0"))
System.out.println("0");
elseif(event.getActionCommand().equals("1"))
System.out.println("1");
elseif(event.getActionCommand().equals("2"))
System.out.println("2");
elseif(event.getActionCommand().equals("3"))
System.out.println("3");
elseif( event.getActionCommand().equals("4"))
System.out.println("4");
elseif(event.getActionCommand().equals("5"))
System.out.println("5");
elseif(event.getActionCommand ().equals("6"))
System.out.println("6");
elseif(event.getActionCommand().equals("7"))
System.out.println("7");
elseif(event.getActionCommand().equals("8"))
System.out.println("8");
elseif(event.getActionCommand().equals("Exit"))
System.exit(0);
else System.out.println("Error in button interface.");
}
publicstaticvoid main(String[] args)
{
TicTacToe buttonGui=new TicTacToe();
buttonGui.setVisible(true);
}
}
right now as you can see i just have the actionPerformed to print the number in the console window cuz i wanted to make sure it works. What do i need to put in the actionPerformed to get an X or an O to print on the button?

