使用JButton从另一个类运行main类

时间:2013-10-24 11:07:11

标签: java swing main

我有一个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]);        
}

它给了我一个空白框架,我想也许它是参数,因为它在main方法中运行得很好所以我创建了一个变量和方法来从main方法返回args但它没有解决问题。 / p>

为什么它不能起作用的任何提示或解释?

完整代码:

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

2 个答案:

答案 0 :(得分:6)

这是一段有问题的代码:

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

从动作侦听器调用时,您处于事件派发线程中。当playgame为真时,该代码在循环中运行,永远不会让控制权返回到EDT,以便它可以进行绘图。

你应该看起来像这样:

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()将代码推送到事件调度线程,这是必需,用于线程安全访问和创建swing组件,因此即使代码看起来有效,也不要跳过它没有它。

此外,如果你根本没有从动作监听器中调用main(),那么它可能会更清晰,但只是启动新游戏的方法。

答案 1 :(得分:0)

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

private void TetrisButtonActionPerformed(java.awt.event.ActionEvent evt) {    
         new Tetris();
    }