-6

My GUI is not working for my tic tac toe game...I would like to find out what i am doing wrong/what is going wrong and why this is not working the way it should? The game should have the nine boxes and for the tic tac toe game and then be able to click on the boxes to have X's and O's until someone wins.

that's pretty much my problem right now. As described in the question, the GUI doesn't work. It asks for player names and which letter you would like to be, X or O. And thats it. I am wondering why the GUI with the nine boxes don't show up.

here is my code:


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.JOptionPane;

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

//Tic Tac Toe


@SuppressWarnings("serial")
public class TicTacToeSwing extends JFrame implements ActionListener {  

//JButtons

//private JButton button1 = new JButton("");    
private JButton jbtnTicTacToe1;
private JButton jbtnTicTacToe2;
private JButton jbtnTicTacToe3;
private JButton jbtnTicTacToe4;
private JButton jbtnTicTacToe5;
private JButton jbtnTicTacToe6;
private JButton jbtnTicTacToe7;
private JButton jbtnTicTacToe8;
private JButton jbtnTicTacToe9;

private JButton jbtnExit = new JButton("Exit"); 
private JButton jbtnReset = new JButton("Reset");

//JFrame window = new JFrame("Tic-Tac-Toe Swing ");
private JFrame window = new JFrame("Tic-Tac-Toe");

//labels
JLabel lblTitle = new JLabel("Tic-Tac-Toe!");
private JLabel jlblPlayerX = new JLabel ("X");
private JLabel jlblPlayerO = new JLabel ("O");

//text fields
JTextArea txtMessage = new JTextArea();
JTextField jtfName = new JTextField(20);
private JTextField jtfPlayerX = new JTextField("X");
private JTextField jtfPlayerO = new JTextField("O");

//Panels
JPanel jpnlMain = new JPanel ();
    JPanel jpnlFamily = new JPanel();
    JPanel jpnlNorth = new JPanel();
    JPanel jpnlSouth = new JPanel();
    JPanel jpnlCenter = new JPanel();
    JPanel jpnlTop = new JPanel();
    JPanel jpnlBottom = new JPanel();

//Class Constructor
public TicTacToeSwing () {

    //Prepare JFrame/Window
    super ("Tic Tac Toe");
    setSize(400,400);
    setTitle("Tic Tac Toe Swing");
    setDefaultCloseOperation(JF3rame.EXIT_ON_CLOSE);

    String n1 = JOptionPane.showInputDialog("Enter Player X name");
    String n2 = JOptionPane.showInputDialog("Enter Player O name"); 

    String input = JOptionPane.showInputDialog("Please select player X or player O: \n1) X\n2) O"); }

    private int Player1;
    private int Player2;

    //Calculate Who's turn is it

    public void actionPerformed1(ActionEvent a) {    
        int count = 0;

    if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
    int letter = Player1;
    }

    else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
    int letter = Player2;
    }


    //Set the layouts for 3 rows and 3 columns
    jpnlMain.setLayout(new BorderLayout(3,3));
    jpnlCenter.setLayout(new GridLayout());
    jpnlSouth.setLayout(new GridLayout());
    jpnlTop.setLayout(new BorderLayout());
    jpnlBottom.setLayout(new BorderLayout());

    //Center Panel
    jpnlCenter.add(jlblPlayerX);
    jpnlCenter.add(jlblPlayerO);

    //identify each JButton
    jbtnReset.setActionCommand("Reset");
    jbtnExit.setActionCommand("Exit");

    //register JButton for event handling by using the THIS keyword - THIS class will handle the events
    jbtnReset.addActionListener(this);
    jbtnExit.addActionListener(this);

    /*Add Buttons To The Window*/
    window.add(jbtnTicTacToe1);
    window.add(jbtnTicTacToe2);
    window.add(jbtnTicTacToe3);
    window.add(jbtnTicTacToe4);
    window.add(jbtnTicTacToe5);
    window.add(jbtnTicTacToe6);
    window.add(jbtnTicTacToe7);
    window.add(jbtnTicTacToe8);
    window.add(jbtnTicTacToe9);

    /*Add The Action Listener To The Buttons
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);
    button6.addActionListener(this);
    button7.addActionListener(this);
    button8.addActionListener(this);
    button9.addActionListener(this);
    */

    jbtnTicTacToe1.addActionListener(this);
    jbtnTicTacToe2.addActionListener(this);
    jbtnTicTacToe3.addActionListener(this);
    jbtnTicTacToe4.addActionListener(this);
    jbtnTicTacToe5.addActionListener(this);
    jbtnTicTacToe6.addActionListener(this);
    jbtnTicTacToe7.addActionListener(this);
    jbtnTicTacToe8.addActionListener(this);
    jbtnTicTacToe9.addActionListener(this);

    //South Button Panel
    jpnlSouth.add(jbtnReset);
    jpnlSouth.add(jbtnExit);

    /* Instantiate JButtons, put into a method for efficiency
    jbtn1 = instantiateJButton("1", Color.PINK);
    jbtn2 = instantiateJButton("2", Color.PINK);
    jbtn3 = instantiateJButton("3", Color.PINK);
    jbtn4 = instantiateJButton("4", Color.PINK);
    jbtn5 = instantiateJButton("5", Color.PINK);
    jbtn6 = instantiateJButton("6", Color.PINK);
    jbtn7 = instantiateJButton("7", Color.PINK);
    jbtn8 = instantiateJButton("8", Color.PINK);
    jbtn9 = instantiateJButton("9", Color.PINK);
    */

    //Finalize screen layout and publish to the display
    jpnlMain.add(jpnlCenter, BorderLayout.NORTH);
    jpnlMain.add(jpnlSouth, BorderLayout.CENTER);

    //Prepare the container
    Container ca = getContentPane();
    ca.setBackground (Color.LIGHT_GRAY);
    ca.add(jpnlMain);
    setContentPane (ca);

    setVisible(true);
    //end constructor       
}

//CLASS EVENT HANDLER
public void actionPerformed(java.awt.event.ActionEvent e)
{
    //find out which JButton was pressed by using the Action Command
    String sActionCommand = e.getActionCommand();

    //EXIT JButton
    if (sActionCommand.equals("Exit"))
    {
        System.exit(0);
    }

    //RESET JButton
    else if (sActionCommand == "Reset")
    {
        jtfPlayerX.setText("");
        jtfPlayerO.setText("");
    }
}   //end ACTIONPERFORMED (java.awt.event.ActionEvent e)

/**
 * @param args
 */
public static void main(String[] args) {
    //EXECUTION STARTING POINT

    //TicTacToeSwing TicTacToeSwing = new TicTacToeSwing();

    //TicTacToeSwing TicTacToeObject = new TicTacToeSwing();

    new TicTacToeSwing();

}//end main(String[] args)

}//end TicTacToeSwing class
tooheymomster
  • 101
  • 2
  • 15
  • 1
    You will have a greater chance of getting decent answers if you spend a little time describing your problems in greater detail. – Hovercraft Full Of Eels Apr 24 '13 at 18:10
  • that's pretty much my problem right now. As described in the question, the GUI doesn't work. It asks for player names and which letter you would like to be, X or O. And thats it. I am wondering why the GUI with the nine boxes don't show up. – tooheymomster Apr 24 '13 at 18:12
  • 2
    Examine the answer you got here...http://stackoverflow.com/questions/16151337/java-tic-tac-toe-swing-game-errors and extend it, it has the info you need – Frank Apr 24 '13 at 18:14
  • "Don't show up" is critical information that you did not have in your original post. You appear to be assuming that we understand things that have not been yet told to us, and let me assure you that this is not true. Again, please edit your original post and tell all the details that will help us understand the problem. – Hovercraft Full Of Eels Apr 24 '13 at 18:14
  • 1
    Voting to close this question. – Hovercraft Full Of Eels Apr 24 '13 at 18:15
  • 1
    This code is a messy one. Why would someone extend from JFrame and implement with ActionListener at the same time.. – Praneeth Peiris Apr 24 '13 at 18:18
  • yes, i will edit the question and add in what "don't show up" means. I did assume that someone would understand what I meant in the context of having a problem with my GUI. I come to this site to get help learning and understanding why my code doesn't work, not to be critiqued on my sentence structures. I assume that there are professionals on here that can help me learn by showing me where my mistakes are and I learn through that. My question has not been answered or solved. So voting to close this question does not help me in any way. – tooheymomster Apr 24 '13 at 18:22
  • @tooheymomster: It is your job to tell us what problems are happening in your code. Understand that we're terrible at reading minds and hate making assumptions. My experience here is that the quality of help you receive is directly related to the quality of the question. Also, learning to write good questions is a skill that most of us are not good at initially. If you continue posting questions here and conscientiously strive to improve your questions, they will get better, and the quality of help will get better. So keep at it and your questions will improve as will your coding abilities. – Hovercraft Full Of Eels Apr 24 '13 at 18:25
  • I agree and I apologize but I thought I explained in simple detail, without writing a book what the problem is, so I apologize. I am interested in learning and becoming as good someday as others already are. The only way that will happen is by learning through mistakes and being able to fix them. – tooheymomster Apr 24 '13 at 18:36
  • The first error in your code is that you never call the method `actionPerformed1` that actualy try to draw the grid. Note that the method name finish with 1. – Joan Apr 24 '13 at 18:39

1 Answers1

2

You've got two JFrames, the class itself, and the window variable. One JFrame, window, gets all of the buttons, and the other gets displayed. I suggest that you don't have your class extend JFrame and instead display the window JFrame.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    + `setVisible(true);` is inside the actionPerformed(). So it never shows. – Praneeth Peiris Apr 24 '13 at 18:19
  • 2
    +1, for the basic problem. Still others. The buttons should be added to a panel that uses a GridLayout. Now you are trying to add them directly to the frame, which won't work as you expect. Basically you need to start over and to one thing at a time. Don't try to write the entire program with listeners until you at least have the frame displayed the way you want. Learn to walk before you run. – camickr Apr 24 '13 at 18:21