packagecom.bu_ish;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Graphics;importjava.awt.GridLayout;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjavax.swing.BorderFactory;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;public class MainFrame extendsJFrame {privateCell[][] cells;privateJLabel statusLabel;privateJButton startButton;private charwhoseTurn;private booleanisStarted;privateMainFrame(String title) {super(title);
setSize(360, 360);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel mainPanel= new JPanel(new GridLayout(3, 3));
cells= new Cell[3][3];for (int i = 0; i < 3; ++i) {for (int j = 0; j < 3; ++j) {
cells[i][j]= newCell();
mainPanel.add(cells[i][j]);
}
}
mainPanel.setVisible(true);
add(mainPanel);
JPanel statusPanel= new JPanel(newBorderLayout());
statusLabel= newJLabel();
statusLabel.setHorizontalAlignment(JLabel.CENTER);
statusPanel.add(statusLabel);
startButton= new JButton("开始");
statusPanel.add(startButton, BorderLayout.EAST);
add(statusPanel, BorderLayout.SOUTH);
startButton.addMouseListener(newMouseAdapter() {
@Overridepublic voidmouseClicked(MouseEvent e) {super.mouseClicked(e);
String text=startButton.getText();if (text.equals("开始") || text.equals("重新开始")) {
startButton.setText("已开始");
startButton.setEnabled(false);
statusLabel.setText("O先行");
whoseTurn= ‘O‘;
isStarted= true;for (int i = 0; i < 3; ++i) {for (int j = 0; j < 3; ++j) {
cells[i][j].clear();
}
}
}
}
});
}private booleanisWon() {for (int i = 0; i < 3; ++i) {if (cells[i][0].getToken() == whoseTurn && cells[i][1].getToken() == whoseTurn && cells[i][2].getToken() ==whoseTurn) {return true;
}
}for (int j = 0; j < 3; ++j) {if (cells[0][j].getToken() == whoseTurn && cells[1][j].getToken() == whoseTurn && cells[2][j].getToken() ==whoseTurn) {return true;
}
}if (cells[0][0].getToken() == whoseTurn && cells[1][1].getToken() == whoseTurn && cells[2][2].getToken() ==whoseTurn) {return true;
}if (cells[2][0].getToken() == whoseTurn && cells[1][1].getToken() == whoseTurn && cells[0][2].getToken() ==whoseTurn) {return true;
}return false;
}private booleanisFull() {for (int i = 0; i < 3; ++i) {for (int j = 0; j < 3; ++j) {if (cells[i][j].getToken() == ‘ ‘) {return false;
}
}
}return true;
}private class Cell extendsJPanel {private char token = ‘ ‘;private booleanisTokenSet;privateCell() {
setBackground(Color.BLACK);
setBorder(BorderFactory.createLineBorder(Color.WHITE));
addMouseListener(newMouseAdapter() {
@Overridepublic voidmouseClicked(MouseEvent e) {super.mouseClicked(e);if(isStarted) {
setToken(whoseTurn);if(isWon()) {
statusLabel.setText(whoseTurn+ "胜利!游戏结束^_^");
isStarted= false;
startButton.setText("重新开始");
startButton.setEnabled(true);
}else{if(isFull()) {
statusLabel.setText("平局!游戏结束^_^");
isStarted= false;
startButton.setText("重新开始");
startButton.setEnabled(true);
}else{
whoseTurn= whoseTurn == ‘O‘ ? ‘X‘ : ‘O‘;
statusLabel.setText("轮到" + whoseTurn + "行");
}
}
}
}
});
}
@Overridepublic voidpaint(Graphics g) {super.paint(g);if(isTokenSet) {
g.setColor(Color.WHITE);if (token == ‘O‘) {
g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
}else{int x = getWidth() - 10;int y = getHeight() - 10;
g.drawLine(10, 10, x, y);
g.drawLine(x,10, 10, y);
}
}
}private void setToken(chartoken) {
repaint();this.token =token;
isTokenSet= true;
}private chargetToken() {returntoken;
}private voidclear() {
isTokenSet= false;
token= ‘ ‘;
repaint();
}
}public static voidmain(String[] args) {new MainFrame("TicTacToe").setVisible(true);
}
}