Question

I have a Java project (a Tetris game) that I imported to another project. Basically I want to run the game from a simple button click (in NetBeans JFrame, not a class).

I can make it work this way:

public static void main(String args[])
{
   TicTacToe.main(args)
}

It works fine, but when I try in a button click event:

private void TetrisButtonActionPerformed(java.awt.event.ActionEvent evt) {       
    TicTacToe.main(new String[0]);        
}

it's giving me a blank frame, I thought maybe it's the argument since it's working in the main method just fine so I created a variable and method to return the args from the main method but it didn't fix the problem.

Any tips or explanation to why it can't work?

Full code:

package tictactoe;

import java.awt.Component;
import javax.swing.JOptionPane;

public class TicTacToe {

    public static boolean playerTurn = true;
    public static boolean playerWon = false;
    public static boolean computerWon = false;
    public static boolean playgame = true;

    public static game board = new game();

    public static void main(String[] args) {

       //startGame();

        while(playgame == true)
        {
            if(board.isVisible() == false)
            {
                board.setVisible(true);
            }
        }

    }


    public static void checkforwin()
    {

        //player win
        if(board.button1.getText().equals("X") )
        {
            if(board.button4.getText().equals("X") )
            {
                if(board.button7.getText().equals("X") )
                    {
                        playerWon = true;
                        computerWon = false;
                        JOptionPane.showMessageDialog(null, "Player won!");
                    }
            }
        }

         if(board.button1.getText().equals("X") )
        {
            if(board.button5.getText().equals("X") )
            {
                if(board.button9.getText().equals("X") )
                    {
                        playerWon = true;
                        computerWon = false;
                        JOptionPane.showMessageDialog(null, "Player won!");
                    }
            }
        }

          if(board.button1.getText().equals("X") )
        {
            if(board.button2.getText().equals("X") )
            {
                if(board.button3.getText().equals("X") )
                    {
                        playerWon = true;
                        computerWon = false;
                       JOptionPane.showMessageDialog(null, "Player won!");
                    }
            }
        }

           if(board.button3.getText().equals("X") )
        {
            if(board.button5.getText().equals("X") )
            {
                if(board.button7.getText().equals("X") )
                    {
                        playerWon = true;
                        computerWon = false;
                        JOptionPane.showMessageDialog(null, "Player won!");
                    }
            }
        }

            if(board.button3.getText().equals("X") )
        {
            if(board.button6.getText().equals("X") )
            {
                if(board.button9.getText().equals("X") )
                    {
                        playerWon = true;
                        computerWon = false;
                       JOptionPane.showMessageDialog(null, "Player won!");
                    }
            }
        }

             if(board.button7.getText().equals("X") )
        {
            if(board.button8.getText().equals("X") )
            {
                if(board.button9.getText().equals("X") )
                    {
                        playerWon = true;
                        computerWon = false;
                       JOptionPane.showMessageDialog(null, "Player won!");
                    }
            }
        }

              if(board.button4.getText().equals("X") )
        {
            if(board.button5.getText().equals("X") )
            {
                if(board.button6.getText().equals("X") )
                    {
                        playerWon = true;
                        computerWon = false;
                        JOptionPane.showMessageDialog(null, "Player won!");
                    }
            }
        }

               if(board.button2.getText().equals("X") )
        {
            if(board.button5.getText().equals("X") )
            {
                if(board.button8.getText().equals("X") )
                    {
                        playerWon = true;
                        computerWon = false;
                    JOptionPane.showMessageDialog(null, "Player won!");
                    }
            }
        }

               //computer won
               if(board.button1.getText().equals("O") )
        {
            if(board.button4.getText().equals("O") )
            {
                if(board.button7.getText().equals("O") )
                    {
                        playerWon = false;
                        computerWon = true;
                        JOptionPane.showMessageDialog(null, "Computer won!");
                    }
            }
        }

                      if(board.button1.getText().equals("O") )
        {
            if(board.button5.getText().equals("O") )
            {
                if(board.button9.getText().equals("O") )
                    {
                        playerWon = false;
                        computerWon = true;
                         JOptionPane.showMessageDialog(null, "Computer won!");
                    }
            }
        }

                             if(board.button1.getText().equals("O") )
        {
            if(board.button2.getText().equals("O") )
            {
                if(board.button3.getText().equals("O") )
                    {
                        playerWon = false;
                        computerWon = true;
                         JOptionPane.showMessageDialog(null, "Computer won!");
                    }
            }
        }

                                    if(board.button3.getText().equals("O") )
        {
            if(board.button5.getText().equals("O") )
            {
                if(board.button7.getText().equals("O") )
                    {
                        playerWon = false;
                        computerWon = true;
                         JOptionPane.showMessageDialog(null, "Computer won!");
                    }
            }
        }

                                           if(board.button3.getText().equals("O") )
        {
            if(board.button6.getText().equals("O") )
            {
                if(board.button9.getText().equals("O") )
                    {
                        playerWon = false;
                        computerWon = true;
                         JOptionPane.showMessageDialog(null, "Computer won!");
                    }
            }
        }

                           if(board.button7.getText().equals("O") )
        {
            if(board.button8.getText().equals("O") )
            {
                if(board.button9.getText().equals("O") )
                    {
                        playerWon = false;
                        computerWon = true;
                        JOptionPane.showMessageDialog(null, "Computer won!");
                    }
            }
        }

                                                         if(board.button4.getText().equals("O") )
        {
            if(board.button5.getText().equals("O") )
            {
                if(board.button6.getText().equals("O") )
                    {
                        playerWon = false;
                        computerWon = true;
                         JOptionPane.showMessageDialog(null, "Computer won!");
                    }
            }
        }

                                                                if(board.button2.getText().equals("O") )
        {
            if(board.button5.getText().equals("O") )
            {
                if(board.button8.getText().equals("O") )
                    {
                        playerWon = false;
                        computerWon = true;
                        JOptionPane.showMessageDialog(null, "Computer won!");
                    }
            }
        }

        if(playerWon == true || computerWon == true)
        {
            board.setVisible(false);
            int dialogresult = JOptionPane.showConfirmDialog(null, "Désirez vous jouer une autre partie? ","test",JOptionPane.YES_NO_OPTION);


            //ask user to select if he want to begin a new game
            if(dialogresult == JOptionPane.YES_OPTION)
            {

                playerWon = false;
                computerWon = false;

                board.setVisible(true);

                board.button1.setText("");
                board.button2.setText("");
                board.button3.setText("");
                board.button4.setText("");
                board.button5.setText("");
                board.button6.setText("");
                board.button7.setText("");
                board.button8.setText("");
                board.button9.setText("");


            }
            else
            {
                playgame = false;
            }
        }


    }

    public static void startGame()
    {


         while(playgame == true)
        {
            if(board.isVisible() == false)
            {
                board.setVisible(true);
            }
        }
    }
}
public class game extends JFrame{

    JButton button1 = new JButton();
    JButton button2 = new JButton();
    JButton button3 = new JButton();
    JButton button4 = new JButton();
    JButton button5 = new JButton();
    JButton button6 = new JButton();
    JButton button7 = new JButton();
    JButton button8 = new JButton();
    JButton button9 = new JButton();

    TicTacToe ttt = new TicTacToe();

    public game()
    {
        initComponents();
        button1.setText("");
        button2.setText("");
        button3.setText("");
        button4.setText("");
        button5.setText("");
        button6.setText("");
        button7.setText("");
        button8.setText("");
        button9.setText("");
    }

       private void button1ActionPerformed(ActionEvent e) {
                if(button1.getText().equals("") );
                {
                    if(TicTacToe.playerTurn == true)
                    {
                        button1.setText("X");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = false;
                    }
                    else
                    {
                        button1.setText("O");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = true;
                    }
                }
        }

        private void button2ActionPerformed(ActionEvent e) {
                  if(TicTacToe.playerTurn == true)
                    {
                        button2.setText("X");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = false;
                    }
                    else
                    {
                        button2.setText("O");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = true;
                    }
        }

        private void button3ActionPerformed(ActionEvent e) {
                  if(TicTacToe.playerTurn == true)
                    {
                        button3.setText("X");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = false;
                    }
                    else
                    {
                        button3.setText("O");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = true;
                    }
        }

        private void button4ActionPerformed(ActionEvent e) {
                  if(TicTacToe.playerTurn == true)
                    {
                        button4.setText("X");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = false;
                    }
                    else
                    {
                        button4.setText("O");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = true;
                    }
        }

        private void button5ActionPerformed(ActionEvent e) {
                if(TicTacToe.playerTurn == true)
                    {
                        button5.setText("X");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = false;
                    }
                    else
                    {
                        button5.setText("O");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = true;
                    }
        }

        private void button6ActionPerformed(ActionEvent e) {
                  if(TicTacToe.playerTurn == true)
                    {
                        button6.setText("X");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = false;
                    }
                    else
                    {
                        button6.setText("O");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = true;
                    }
        }

        private void button7ActionPerformed(ActionEvent e) {
                  if(TicTacToe.playerTurn == true)
                    {
                        button7.setText("X");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = false;
                    }
                    else
                    {
                        button7.setText("O");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = true;
                    }
        }

        private void button8ActionPerformed(ActionEvent e) {
                 if(TicTacToe.playerTurn == true)
                    {
                        button8.setText("X");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = false;
                    }
                    else
                    {
                        button8.setText("O");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = true;
                    }
        }

        private void button9ActionPerformed(ActionEvent e) {
                  if(TicTacToe.playerTurn == true)
                    {
                        button9.setText("X");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = false;
                    }
                    else
                    {
                        button9.setText("O");
                        TicTacToe.checkforwin();
                        TicTacToe.playerTurn = true;
                    }
        }
Was it helpful?

Solution

This is the problematic piece of code:

while(playgame == true)
{
    if(board.isVisible() == false)
    {
        board.setVisible(true);
    }
}

When called from the action listener, you're in the event dispatch thread. That code runs in a loop when playgame is true, never letting control back to the EDT so that it could do the drawing.

You main should look something like this instead:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
             // Create board here, if needed
             createBoard();
             board.setVisible(true);
             // start a new game here
             startGame();
         }
     });
}

invokeLater() pushes the code to the event dispath thread, which is required for thread safe access and creation of swing components, so don't skip that even if your code appears to work without it.

Also, it might be cleaner if you simply did not call main() from the action listener at all, but just the method that starts a new game.

OTHER TIPS

I am not sure if you need more than one "main" method in your project. Maybe you should use a default constructor.

private void TetrisButtonActionPerformed(java.awt.event.ActionEvent evt) {    
         new Tetris();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top