import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class TicTacToe extends JApplet
{
private char whoseTurn='X';
private Cell[][] cell=new Cell[3][3];

private JLabel jlblStatus=new JLabel("X's turn to play");

public void init()
{
JPanel p=new JPanel();
p.setLayout(new GridLayout(3,3,0,0));
for(int i=0;i<3;i++)
 for(int j=0;j<3;j++)
  p.add(cell[i][j]=new Cell());
  
p.setBorder(new LineBorder(Color.red,1));
jlblStatus.setBorder(new LineBorder(Color.yellow,1));

this.getContentPane().add(p,BorderLayout.CENTER);
this.getContentPane().add(jlblStatus,BorderLayout.SOUTH);

}
public static void main(String[] args)
{
JFrame frame=new JFrame("Tic Tac Toe");
TicTacToe applet=new TicTacToe();
frame.getContentPane().add(applet,BorderLayout.CENTER);
 
 applet.init();
 applet.start();
 
 frame.setSize(300,300);
 frame.setVisible(true);
 
}
public boolean isFull( )
{
for(int i=0;i<3;i++)
 for(int j=0;j<3;j++)
 if(cell[i][j].getToken()==' ')
  return false;
  
 return true;
}
public boolean isWon(char token)
{
for(int i=0;i<3;i++)
 if((cell[i][0].getToken()==token)&&(cell[i][1].getToken()==token)&&(cell[i][2].getToken()==token))
 {return true;}
 
for(int j=0;j<3;j++)
 if((cell[0][j].getToken()==token)&&(cell[1][j].getToken()==token)&&(cell[2][j].getToken()==token))
 {return true;}
 
 if((cell[0][0].getToken()==token)&&(cell[1][1].getToken()==token)&&(cell[2][2].getToken()==token))
 {return true;}
 
 if((cell[0][2].getToken()==token)&&(cell[1][1].getToken()==token)&&(cell[2][0].getToken()==token))
 {return true;}
 
 return false;

}

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();

}
public void paitComponent(Graphics g)
{
super.paintComponents(g);
if(token=='X')
{
g.drawLine(10,10,getSize().width-10,getSize().height-10);
g.drawLine(getSize().width-10,10,10,getSize().height-10);
}
else if(token=='0')
{
g.drawOval(10,10,getSize().width-10,getSize().height-20);

}

}
public void mouseClicked(MouseEvent e)
{
if(token==' ')
{
if(whoseTurn=='X')
{
setToken('X');
whoseTurn=0;
jlblStatus.setText("O's turn");
    
    if(isWon('X'))
     jlblStatus.setText("X won!The game is over");
     
    else if(isFull())
    jlblStatus.setText("Draw!The game is over");
    
}
   else if(whoseTurn=='O')
   {
    setToken('O');
    whoseTurn='X';
    jlblStatus.setText("O's turn");
    
  if(isWon('O'))
   jlblStatus.setText("O won!The game is over");
     
  else if(isFull())
  jlblStatus.setText("Draw!The game is over");
   
    }
}
}
public void mousePressed(MouseEvent e)
{

}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}

}
}
怎么只显示了框架而已啊?

解决方案 »

  1.   

    public void paintComponent(Graphics g) {
    super.paintComponents(g);
    if (token == 'X') {
    g.drawLine(10, 10, getSize().width - 10, getSize().height - 10);
    g.drawLine(getSize().width - 10, 10, 10, getSize().height - 10);
    } else if (token == '0') {
    g.drawOval(10, 10, getSize().width - 10, getSize().height - 20);
    }
    }
    方法名错啦
      

  2.   

    重写方法的时候
    加上@Override注释可以避免类似错误
      

  3.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.LineBorder;public class TicTacToe extends JApplet {
    private char whoseTurn = 'X';
    private Cell[][] cell = new Cell[3][3];
    private JLabel jlblStatus = new JLabel("X's turn to play"); public void init() {
    JPanel p = new JPanel();
    p.setLayout(new GridLayout(3, 3, 0, 0));
    for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
    p.add(cell[i][j] = new Cell()); p.setBorder(new LineBorder(Color.red, 1));
    jlblStatus.setBorder(new LineBorder(Color.yellow, 1));
    this.getContentPane().add(p, BorderLayout.CENTER);
    this.getContentPane().add(jlblStatus, BorderLayout.SOUTH); } public static void main(String[] args) {
    JFrame frame = new JFrame("Tic Tac Toe");
    TicTacToe applet = new TicTacToe();
    frame.getContentPane().add(applet, BorderLayout.CENTER); applet.init();
    applet.start(); frame.setSize(300, 300);
    frame.setVisible(true);
    } public boolean isFull() {
    for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
    if (cell[i][j].getToken() == ' ')
    return false;
    return true;
    } public boolean isWon(char token) {
    for (int i = 0; i < 3; i++)
    if ((cell[i][0].getToken() == token)
    && (cell[i][1].getToken() == token)
    && (cell[i][2].getToken() == token)) {
    return true;
    } for (int j = 0; j < 3; j++)
    if ((cell[0][j].getToken() == token)
    && (cell[1][j].getToken() == token)
    && (cell[2][j].getToken() == token)) {
    return true;
    } if ((cell[0][0].getToken() == token)
    && (cell[1][1].getToken() == token)
    && (cell[2][2].getToken() == token)) {
    return true;
    } if ((cell[0][2].getToken() == token)
    && (cell[1][1].getToken() == token)
    && (cell[2][0].getToken() == token)) {
    return true;
    } return false; } 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();
    } @Override
    public void paint(Graphics g) {
    super.paint(g);
    if (token == 'X') {
    g.drawLine(10, 10, getSize().width - 10, getSize().height - 10);
    g.drawLine(getSize().width - 10, 10, 10, getSize().height - 10);
    } else if (token == 'O') {
    g.drawOval(10, 10, getSize().width - 16, getSize().height - 20);
    }
    } public void mouseClicked(MouseEvent e) {
    if (token == ' ') {
    if (whoseTurn == 'X') {
    setToken('X');
    whoseTurn = 'O';
    jlblStatus.setText("O's turn");
    if (isWon('X'))
    jlblStatus.setText("X won!The game is over");
    else if (isFull())
    jlblStatus.setText("Draw!The game is over");
    } else if (whoseTurn == 'O') {
    setToken('O');
    whoseTurn = 'X';
    jlblStatus.setText("X's turn");
    if (isWon('O'))
    jlblStatus.setText("O won!The game is over");
    else if (isFull())
    jlblStatus.setText("Draw!The game is over");
    }
    }
    } public void mousePressed(MouseEvent e) {
    } public void mouseReleased(MouseEvent e) {
    } public void mouseEntered(MouseEvent e) {
    } public void mouseExited(MouseEvent e) {
    }
    }
    }程序里不少笔误的地方
    改了下给你
      

  4.   


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    public class TicTacToe extends JApplet
    {
        int flag=0;
        private char whoseTurn='X';
        private Cell[][] cell=new Cell[3][3];
        
        private JLabel jlblStatus=new JLabel("X's turn to play");
        
        public void init()
        {
            JPanel p=new JPanel();
            p.setLayout(new GridLayout(3,3,0,0));
            for(int i=0;i<3;i++)
             for(int j=0;j<3;j++)
              p.add(cell[i][j]=new Cell());
              
            p.setBorder(new LineBorder(Color.red,1));
            jlblStatus.setBorder(new LineBorder(Color.yellow,1));
            
            this.getContentPane().add(p,BorderLayout.CENTER);
            this.getContentPane().add(jlblStatus,BorderLayout.SOUTH);
            
            }
        public static void main(String[] args)
        {
            JFrame frame=new JFrame("Tic Tac Toe");
            TicTacToe applet=new TicTacToe();
            frame.getContentPane().add(applet,BorderLayout.CENTER);
             
             applet.init();
             applet.start();
             
             frame.setSize(300,300);
             frame.setVisible(true);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        public boolean isFull( )
        {
            for(int i=0;i<3;i++)
             for(int j=0;j<3;j++)
             if(cell[i][j].getToken()==' ')
              return false;
              
             return true;
            }
        public boolean isWon(char token)
        {
            for(int i=0;i<3;i++)
             if((cell[i][0].getToken()==token)&&(cell[i][1].getToken()==token)&&(cell[i][2].getToken()==token))
             {return true;}
             
            for(int j=0;j<3;j++)
             if((cell[0][j].getToken()==token)&&(cell[1][j].getToken()==token)&&(cell[2][j].getToken()==token))
             {return true;}
             
             if((cell[0][0].getToken()==token)&&(cell[1][1].getToken()==token)&&(cell[2][2].getToken()==token))
             {return true;}
             
             if((cell[0][2].getToken()==token)&&(cell[1][1].getToken()==token)&&(cell[2][0].getToken()==token))
             {return true;}
             
             return false;
            
        }
        
        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();
                }
            public void paintComponent(Graphics g)
            {
                super.paintComponents(g);
                if(token=='X')
                {
                    g.drawLine(10,10,getSize().width-10,getSize().height-10);
                    g.drawLine(getSize().width-10,10,10,getSize().height-10);
                }
                else if(token=='O')
                {
                    g.drawOval(10,10,getSize().width-10,getSize().height-20);
                    
                    }
                
            }
            public void mouseClicked(MouseEvent e)
            {
                if(flag==1)
                    return;
                if(token==' ')
                {
                    if(whoseTurn=='X')
                    {
                        setToken('X');
                        whoseTurn='O';
                        jlblStatus.setText("O's turn");
                        
                        if(isWon('X')) {
                         jlblStatus.setText("X won!The game is over");
                         flag=1;
                        }
                         
                        else if(isFull())
                        jlblStatus.setText("Draw!The game is over");
                        
                        }
                   else if(whoseTurn=='O')
                   {
                       setToken('O');
                       whoseTurn='X';
                       jlblStatus.setText("O's turn");
                        
                      if(isWon('O')) {
                       jlblStatus.setText("O won!The game is over");
                       flag=1;
                      }
                         
                      else if(isFull())
                      jlblStatus.setText("Draw!The game is over");
                       
                       }
                    }
                }
            public void mousePressed(MouseEvent e)
            {
                
                }
            public void mouseReleased(MouseEvent e)
            {
                }
            public void mouseEntered(MouseEvent e)
            {
                }
            public void mouseExited(MouseEvent e)
            {
                }
        
                }
            }