import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
//创建TicTacToe类来控制并处理所有单元格
public class TicTacToe extends JApplet

  //用来记录下一个放到单元格的标记的种类
  private char whoseTurn='X';
  private Cell[][] cells=new Cell[3][3];
  //用来显示游戏的状态
  private JLabel jlblStaus=new JLabel("X's turn to play");
  
  public TicTacToe()
  {
  JPanel p=new JPanel(new GridLayout(3,3,0,0));
  for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
  p.add(cells[i][j]=new Cell());
  
  p.setBorder(new LineBorder(Color.red,1));//创建具有指定颜色和厚度的线边框。
  jlblStaus.setBorder(new LineBorder(Color.yellow,1));//创建具有指定颜色和厚度的线边框。
  //把面板跟标签加到applet
  this.getContentPane().add(p,BorderLayout.CENTER);
  this.getContentPane().add(jlblStaus,BorderLayout.SOUTH);
  }
  
  //判断游戏的状态,用来判断是否所有的单元格都被占满了
  public boolean isFull()
  {
  for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
  if(cells[i][j].getToken() ==' ')
  return false;
  
  return true;
  } 
  
  //判断指定标记的游戏者是否是胜者
  public boolean isWon(char token)
  {
  for(int i=0;i<3;i++)
    if((cells[i][0].getToken()==token)&&(cells[i][1].getToken()==token)&&(cells[i][2].getToken()==token))
    {
     return true;
    }
  for(int j=0;j<3;j++)
  if((cells[0][j].getToken()==token)&&(cells[1][j].getToken()==token)&&(cells[2][j].getToken()==token))
    {
     return true;
    }  
  if((cells[0][0].getToken()==token)&&(cells[1][1].getToken()==token)&&(cells[2][2].getToken()==token))
    {
     return true;
    }
  if((cells[0][2].getToken()==token)&&(cells[1][1].getToken()==token)&&(cells[2][0].getToken()==token))
    {
     return true;
    }
  return false;
  }
 
  
  //创建一个cell来处理单个单元的格操作
  public class Cell extends JPanel implements MouseListener
  {
  private char token=' ';
  
  public Cell()
  {
  setBorder(new LineBorder(Color.black,1));
  addMouseListener(this);
  }
  
  public char getToken()
  {
  return token;
  }
  
  public void setToken(char c)
  {
  token=c;
  repaint();
  }
  
  protected void painComponent(Graphics g)
  {
  super.paintComponent(g);
  if(token == 'X')
  {
 g.drawLine(10,10,getWidth() - 10,getHeight() - 10);
     g.drawLine(getWidth() - 10,10,10,getHeight() - 10);
  }
  else if(token == '0')
  {
 g.drawOval(10,10,getWidth() - 20,getHeight() - 20);  
  }
  }
  
  public void mouseClicked(MouseEvent e)
  {
  if(token == ' ' && whoseTurn != ' ')
  { 
  setToken(whoseTurn);
  
  if(isWon(whoseTurn))
  {
  jlblStaus.setText(whoseTurn + "  won! The game is over");
  whoseTurn=' ';
  }
  else if(isFull())
  {
  jlblStaus.setText("Draw! The game is over");
  whoseTurn=' ';
  }
  else
  {
  whoseTurn = (whoseTurn == 'X') ? '0': 'X';
  jlblStaus.setText(whoseTurn+ " 's turn");
  }
  }
  }
  //在源组件上按下鼠标键时调用
  public void mousePressed(MouseEvent e)
  {
  
  }
  //在源组件上释放鼠标键时调用
  public void mouseReleased(MouseEvent e)
  {
  
  }
  //当鼠标指针进入源组件时调用
  public void mouseEntered(MouseEvent e)
  {
  
  }
  //当鼠标指针移出源组件时调用
  public void mouseExited(MouseEvent e)
  {
  
  }
  }   
}
为什么游戏可以运行但是不能出现 X跟0。