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)
{
}
}
}
怎么只显示了框架而已啊?
解决方案 »
- 一个正则表达式的匹配输出问题
- 一个异常问题
- 紧急求救: 如何实现窗口中的组件大小随其窗口大小的改变而改变?
- java流怎么close最合适
- JAVA中如何生成TXT文件并且写入一些 字符串?
- 请问哪里有介绍JCreator的资料?
- 将类firstClass声明在另一个类Test内部声明,编绎时报错:non-static variable this cannot be referenced from a static contex
- JAVA SOCKET客户端如何实时和服务器端进行通讯?用线程吗?
- Image问题,求各位大侠帮帮我吧。
- 有在UNIX下调试JAVA程序经验的富有同情心的同人来帮我一把!呜呜呜!
- DatagramSocket的receive问题
- 字符串解析的问题
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);
}
}
方法名错啦
加上@Override注释可以避免类似错误
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) {
}
}
}程序里不少笔误的地方
改了下给你
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)
{
}
}
}