I am trying to make a tic tac toe game in java but my GUI is just not showing up. It makes a board and you are able to press buttons to play tic tac toe. I am new to java so I have no idea what is wrong. It has no errors and I use repl. What is wrong?
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.GridLayout;
public class Main extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel panel = new JPanel();
private XOButtons[] buttons = new XOButtons[9];
private int turn = 0;
@Override
public void actionPerformed(ActionEvent actionEvent) {
XOButtons source = (XOButtons)actionEvent.getSource();
if(turn == 0){
source.toggleX();
setTitle("O Turn");
} else if(turn == 1){
source.toggleO();
setTitle("X's Turn");
}
turn = (turn + 1) % 2;
}
public static void main(String[] args) {
new TicTacToe();
}
public void TicTacToe() {
setTitle("Tic Tac Toe");
setSize(600,600);
setLocation(100,100);
getContentPane().setBackground(Color.CYAN);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel.setLayout(new GridLayout(3,3,5,5));
panel.setBackground(Color.BLUE);
for(int i=0; i < buttons.length; i++) {
buttons[i] = new XOButtons();
buttons[i].addActionListener(this);
panel.add(buttons[i]);
}
add(panel);
setVisible(true);
}
}
Here is my second java file:
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.ImageIcon;
public class XOButtons extends JButton {
private static final long serialVersionUID = 1L;
private ImageIcon xIcon = new ImageIcon(getClass().getResource("x.png"));
private ImageIcon oIcon = new ImageIcon(getClass().getResource("o.png"));
public void toggleX() {
if(getIcon() == null) {
setIcon(xIcon);
} else {
setIcon(null);
}
}
public void toggleO() {
if(getIcon() == null) {
setIcon(oIcon);
} else {
setIcon(null);
}
}
}
Also, I have x.png and o.png loaded in.