1

我有一个导入到另一个项目的 Java 项目(俄罗斯方块游戏)。基本上我想通过一个简单的按钮单击运行游戏(在 NetBeans JFrame 中,而不是一个类)。

我可以让它这样工作:

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

它工作正常,但是当我尝试按钮单击事件时:

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

它给了我一个空白框架,我想这可能是参数,因为它在主方法中工作得很好,所以我创建了一个变量和方法来从主方法返回参数,但它没有解决问题。

任何提示或解释为什么它不能工作?

完整代码:

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;
                    }
        }
4

2 回答 2

6

这是有问题的一段代码:

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

当从动作侦听器调用时,您处于事件调度线程中。该代码在playgame为真时循环运行,从不将控制权交还给 EDT,以便它可以进行绘图。

您的 main 应该看起来像这样:

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()将代码推送到 event dispath 线程,这是线程安全访问和创建 swing 组件所必需的,因此即使您的代码似乎在没有它的情况下也可以工作,也不要跳过它。

此外,如果您根本不main()从动作侦听器调用,而只是从启动新游戏的方法调用,它可能会更干净。

于 2013-10-24T11:33:57.570 回答
0

我不确定您的项目中是否需要多个“主要”方法。也许您应该使用默认构造函数。

private void TetrisButtonActionPerformed(java.awt.event.ActionEvent evt) {    
         new Tetris();
    }
于 2013-10-24T11:31:57.350 回答