Hi everyone,
So hopefully I go about this right, and I can figure out what I'm doing wrong here. As an exercise, I'm trying to write a Tic-Tac-Toe (TTT) game. However, in the end it will be adaptable for different variations of TTT, so it's broken up some. I have a TTTGame.java, and TTTSquareFrame.java, and some others that aren't relavent.
So, TTTGame:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import joshPack.jUtil.*;
public class TTTGame extends JFrame
{
private Integer sides = 3;
private TTTSquareFrame mainSquare;
private TTTGame newGame;
private Container contents;
private JPanel mainSquarePanel, addPanel;
public static void main(String [] args)
{
TTTGame newGame = new TTTGame();
newGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public TTTGame()
{
super("Tic-Tac-Toe");
contents = getContentPane();
contents.setLayout(new FlowLayout());
addPanel = startSimple();
if(!addPanel.isValid())
{
System.out.println("Something's wrong");
}
contents.add(addPanel);
setSize(300, 300);
setVisible(true);
}
public JPanel startSimple()
{
mainSquare = new TTTSquareFrame(sides);
mainSquarePanel = mainSquare.createPanel(sides);
return mainSquarePanel;
}
}
and TTTSquareFrame:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import joshPack.jUtil.Misc;
public class TTTSquareFrame
{
private JPanel squarePanel;
private JButton [] squares;
private int square, index;
public TTTSquareFrame()
{
System.out.println("Use a constructor that passes an integer specifying the size of the square please.");
System.exit(0);
}
public TTTSquareFrame(int size)
{
}
public JPanel createPanel(int size)
{
square = (int)Math.pow(size, 2);
squarePanel = new JPanel();
squarePanel.setLayout(new GridLayout(3,3));
squares = new JButton[square];
System.out.println(MIN_SIZE.toString());
for(int i = 0; i < square; i++)
{
squares[i] = new JButton();
squares.setRolloverEnabled(false);
squares[i].addActionListener(bh);
//squares[i].setMinimumSize(MIN_SIZE);
squares[i].setVisible(true);
squarePanel.add(squares[i]);
}
squarePanel.setSize(100, 100);
squarePanel.setVisible(true);
return squarePanel;
}
}I've successfully added panels to JFrame within the same class, and this is the first time I'm modularizing the code this way. The issue is that the frame comes up blank, and I get the message "Something's wrong" and it says the addPanel is invalid. Originally, the panel creation was in the constructor for TTTSquareFrame, and I just added the mainSquare (from TTTGame class) to the content pane, when that didn't work, I tried going about it this way. Not exactly sure why I wouldn't be able to add the panel from another class, any help is greatly appreciated.
I did try and cut out code that wasn't needed, if it's still too much let me know and I can try and whittle it down more. Thanks.