-1

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.

hello
  • 1
  • 2
    That code does not even compile. There is no constructor or even class that matches `new TicTacToe();` – f1sh Apr 28 '21 at 13:40
  • Am I supposed to replace it with something like JFrame? – hello Apr 28 '21 at 14:02
  • 2
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! Most IDEs have a keyboard shortcut specifically for formatting code. – Andrew Thompson Apr 28 '21 at 14:04
  • 1
    3) One way to get image(s) for an MRE / SSCCE (as appears in [my answer](https://stackoverflow.com/a/67302034/418556)) is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Apr 28 '21 at 14:24

1 Answers1

1

There are a few problems and less than optimal ways of solving the problem in that code, but this problem was caused by turning the class name (TicTacToe) into Main and changing the constructor into a method.

This is what results when that is corrected.

enter image description here

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

public class TicTacToe extends JFrame implements ActionListener {

    private final JPanel panel = new JPanel();
    private final XOButtons[] buttons = new XOButtons[9];
    private int turn = 0;

    public 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);
    }

    @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();
    }
}

class XOButtons extends JButton {

    private ImageIcon xIcon = null;
    private ImageIcon oIcon = null;

    XOButtons() {
        try {
            xIcon = new ImageIcon(new URL("https://i.stack.imgur.com/in9g1.png"));
            oIcon = new ImageIcon(new URL("https://i.stack.imgur.com/wCF8S.png"));
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    }

    public void toggleX() {
        if (getIcon() == null) {
            setIcon(xIcon);
        } else {
            setIcon(null);
        }
    }

    public void toggleO() {
        if (getIcon() == null) {
            setIcon(oIcon);
        } else {
            setIcon(null);
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks but it might be something wrong with repl as it says that there is an invalid method declaration; return type required where it says public TicTacToe() { – hello Apr 28 '21 at 14:43
  • *"it says that there is an invalid method declaration; return type required where it says public TicTacToe()"* In the code in the answer, or in your code? (I can probably guess, but guesses aren't information.) – Andrew Thompson Apr 28 '21 at 14:50
  • In your answer. I copied your code and put it into repl. – hello Apr 28 '21 at 15:12
  • What exactly is 'repl'? I suggest you start an entirely new project, paste the code in my answer, and confirm it works. – Andrew Thompson Apr 28 '21 at 23:55