MainFrame.java

package com.bu_ish;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel; public class MainFrame extends JFrame { private Cell[][] cells;
private JLabel statusLabel;
private JButton startButton;
private char whoseTurn;
private boolean isStarted; private MainFrame(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] = new Cell();
mainPanel.add(cells[i][j]);
}
}
mainPanel.setVisible(true);
add(mainPanel);
JPanel statusPanel = new JPanel(new BorderLayout());
statusLabel = new JLabel();
statusLabel.setHorizontalAlignment(JLabel.CENTER);
statusPanel.add(statusLabel);
startButton = new JButton("开始");
statusPanel.add(startButton, BorderLayout.EAST);
add(statusPanel, BorderLayout.SOUTH);
startButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(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 boolean isWon() {
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 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;
} private class Cell extends JPanel { private char token = ' ';
private boolean isTokenSet; private Cell() {
setBackground(Color.BLACK);
setBorder(BorderFactory.createLineBorder(Color.WHITE));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(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 + "行");
}
}
}
}
});
} @Override
public void paint(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(char token) {
repaint();
this.token = token;
isTokenSet = true;
} private char getToken() {
return token;
} private void clear() {
isTokenSet = false;
token = ' ';
repaint();
}
} public static void main(String[] args) {
new MainFrame("TicTacToe").setVisible(true);
}
}

最新文章

  1. 如何安装并使用hibernate tools
  2. spring mvc 工作流程
  3. JAVA内存机制
  4. 认真学习shell的第一天-数学运算
  5. General: Know How to Use InetAddress
  6. hdu 1199 Color the Ball(离散化线段树)
  7. Java CopyOnWriteArrayList分析
  8. HTML在Select具体的使用说明
  9. SQL SERVER与C#的数据类型对应表
  10. php 实现购物车功能,以大苹果购物网为例,上图上代码。。。。
  11. mac os 安装 wget
  12. 《UNIX实用教程》读书笔记
  13. goland 文件头自动注释
  14. .NET Core整理之配置EFCore
  15. tofixed方法 四舍五入
  16. 如何删除新添加的项目Module
  17. Python-mysql 权限 pymysql 注入共计
  18. vue js校验金钱、数字
  19. 你可能不知道的git clean
  20. 网络对抗——web基础

热门文章

  1. sqlite 使用
  2. Linux命令文件查看过滤
  3. Linux 之 软件安装-yum、rpm、源码安装
  4. ELK之收集Java日志、通过TCP收集日志
  5. android添加桌面悬浮窗
  6. java并发之hashmap
  7. POJ1430 Binary Stirling Numbers
  8. Ubuntu 16.04下使用Wine安装PowerDesigner15
  9. Jena+fuseki
  10. 【hibernate】Hibernate中get()和load()的区别