Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的Tic Tac Toe重置按钮_Java_Jtextarea_Tic Tac Toe - Fatal编程技术网

java中的Tic Tac Toe重置按钮

java中的Tic Tac Toe重置按钮,java,jtextarea,tic-tac-toe,Java,Jtextarea,Tic Tac Toe,我已经添加了一个按钮来休息游戏,但我不知道如何添加一个侦听器,或者当我点击按钮时如何调用一个方法。请告诉我如何使复位按钮工作。多谢各位 private String[] board = new String[ 9 ]; // tic-tac-toe board private JTextArea outputArea; // for outputting moves private Player[] players; // array of Players private

我已经添加了一个按钮来休息游戏,但我不知道如何添加一个侦听器,或者当我点击按钮时如何调用一个方法。请告诉我如何使复位按钮工作。多谢各位

   private String[] board = new String[ 9 ]; // tic-tac-toe board
   private JTextArea outputArea; // for outputting moves
   private Player[] players; // array of Players
  private ServerSocket server; // server socket to connect with clients
  private int currentPlayer; // keeps track of player with current move
  private final static int PLAYER_X = 0; // constant for first player
   private final static int PLAYER_O = 1; // constant for second player
  private final static String[] MARKS = { "X", "O" }; // array of marks
  private ExecutorService runGame; // will run players
  private Lock gameLock; // to lock game for synchronization
  private Condition otherPlayerConnected; // to wait for other player
  private Condition otherPlayerTurn; // to wait for other player's turn
  private boolean win;
 public TicTacToeServer()
  {
  super( "Tic-Tac-Toe Server" ); // set title of window

  // create ExecutorService with a thread for each player
  runGame = Executors.newFixedThreadPool( 2 );
  gameLock = new ReentrantLock(); // create lock for game

  // condition variable for both players being connected
  otherPlayerConnected = gameLock.newCondition();

  // condition variable for the other player's turn
  otherPlayerTurn = gameLock.newCondition();      

  for ( int i = 0; i < 9; i++ )
     board[ i ] = new String( "" ); 
  players = new Player[ 2 ]; // create array of players
  currentPlayer = PLAYER_X; // set current player to first player

  try
  {
     server = new ServerSocket( 12345, 2 ); // set up ServerSocket
  } // end try
  catch ( IOException ioException ) 
  {
     ioException.printStackTrace();
     System.exit( 1 );
  } // end catch

  outputArea = new JTextArea(); // create JTextArea for output
  add( outputArea, BorderLayout.CENTER );
  JButton reset= new JButton ("Reset");
  add(reset, BorderLayout.SOUTH);
  reset.setActionCommand("reset");
  //reset.addActionListener(this);
  outputArea.setText( "Server awaiting connections\n" );


  setSize( 300, 300 ); // set size of window
  setVisible( true ); // show window
  } // end TicTacToeServer constructor

您应该使用ActionListener:

将其注册到按钮,一个实现将包含在单击按钮后自动调用的代码。 这就是java事件系统的工作原理


下面是一个教程

您可以像这样简单地添加一个动作列表器

   reset.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e)
      {
         //Execute when button is pressed
      }        
    });      

我添加了侦听器和execute,但什么也没发生:reset.setActionCommandreset;reset.addActionListener新建ActionListener{public void actionPerformedActionEvent e{for int i=0;i<9;i++板[i]=新字符串;玩家=新玩家[2];currentPlayer=Player_X;Player} };当一名球员转身时,你如何重新粉刷棋盘。