1

ゲームを停止するボタンを追加しましたが、リスナーを追加する方法や、ボタンをクリックしたときにメソッドを呼び出す方法がわかりません。リセットボタンを有効にする方法を教えてください。ありがとうございました。

   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
4

2 に答える 2

0

ActionListener を使用する必要があります。

それをボタンに登録すると、ボタンがクリックされると自動的に呼び出されるコードが実装に含まれます。これがJavaイベントシステムの仕組みです。

これがチュートリアルアクションリスナーです

于 2012-07-18T06:25:58.090 に答える