Java实现简单的五子棋(swing图形化界面)

*public class TableFace *

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class TableFace {

	JFrame frame;
	static int width;
	static int height;
	
	static {
//		获得屏幕的尺寸
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		width = d.width;
		height = d.height;	
	}
	
//	下棋顺序		黑棋先手
	static boolean hand = true;
	
	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TableFace window = new TableFace();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public TableFace() {
		initialize();
	}

	
//	定义棋盘
//	private MyJLabel[][] chessMap = {
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null},
//			{null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null}
//			};
	
	private MyJLabel[][] chessMap = new MyJLabel[25][25];

	private void initialize() {
		frame = new JFrame();
		frame.setSize(width, height);
		frame.getContentPane().setLayout(new GridLayout(25, 25));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		for(int i = 0;i < 625;i++) {
			MyJLabel lab = new MyJLabel("+",i);
			lab.setHorizontalAlignment(JLabel.CENTER);
			lab.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
//					获得待落子的位置信息
					MyJLabel my = (MyJLabel) e.getSource();
//					获取位置的数值
					int pos = my.getCurrentNo();
//					获取行数
					int row = pos / 25;
//					获取列数
					int col = pos % 25;
//					得到 chessMap 上的坐标
					MyJLabel fromMap = chessMap[row][col];
//					System.out.println(row + "\t" +col);
//					检测位置上是否存在棋子
//					1、该位置存在棋子					
					if(fromMap != null) {
						return;
					}
//					2、该位置不存在棋子
//					清空  + 
					my.setText("");
//					落子无悔
					if(hand) {
						Icon img = new ImageIcon(TableFace.class.getResource("/").getFile() + "black.png");
						my.setIcon(img);
						my.setType("Black");
						hand = false;
					}else {
						Icon img = new ImageIcon(TableFace.class.getResource("/").getFile() + "white.png");
						my.setIcon(img);
						my.setType("White");
						hand = true;
					}
					
//					记录已落子的位置
					chessMap[row][col] = my;
//					记录判断结果
					boolean back = calChessCount(my,row,col);
					if(back == true) {
						JOptionPane.showMessageDialog(null, my.getType() +"\nWIN!");
						TableFace.this.frame.setVisible(false);
						new WinInterface().frame.setVisible(true);
						TableFace.this.frame.dispose();
					}
				}
				
			});
			frame.getContentPane().add(lab);
		}

	}

//	判断棋子个数
	protected boolean calChessCount(MyJLabel myChess, int row, int col) {
//		初始化棋子个数
		int count = 1;
		
//		定义X轴方向的临时变量
		int tempX = 1;
//		定义Y轴方向的临时变量
		int tempY = 1;
		
//		计算落子的上方棋子个数
		while(row > 0){
			int y = row - tempY;
			MyJLabel chess = chessMap[y][col];
//			上方为null	上方棋子  type 不同
			if(chess == null || chess.getType().equals(myChess.getType()) == false) {
				break;
			}
//			记录个数
			count++;
//			继续向上查找
			tempY++;
//			判断结果
			if(count == 5) {
				return true;
			}
			
		}
		
//		重置Y轴方向的临时变量
		tempY = 1;
//		计算落子的下方棋子个数
		while(row < 25){
			int y = row + tempY;
			MyJLabel chess = chessMap[y][col];
//			上方为null	上方棋子  type 不同
			if(chess == null || chess.getType().equals(myChess.getType()) == false) {
				break;
			}
//			记录个数
			count++;
//			继续向上查找
			tempY++;
//			判断结果
			if(count == 5) {
				return true;
			}
		}
		
		
//		重置Y轴方向的临时变量
		tempY = 1;
//		计算落子的右上方棋子个数
		while(row > 0 && col < 25){
			int y = row - tempY;
			int x = col + tempX;
			MyJLabel chess = chessMap[y][x];
//			上方为null	上方棋子  type 不同
			if(chess == null || chess.getType().equals(myChess.getType()) == false) {
				break;
			}
//			记录个数
			count++;
//			继续向上查找
			tempY++;
			tempX++;
//			判断结果
			if(count == 5) {
				return true;
			}
		}
		
		
//		重置Y轴方向的临时变量
		tempY = 1;
//		重置X轴方向的临时变量
		tempX = 1;
//		计算落子的左下方棋子个数
		while(row < 25 && col > 0){
			int y = row + tempY;
			int x = col - tempX;
			MyJLabel chess = chessMap[y][x];
//			上方为null	上方棋子  type 不同
			if(chess == null || chess.getType().equals(myChess.getType()) == false) {
				break;
			}
//			记录个数
			count++;
//			继续向上查找
			tempY++;
			tempX++;
//			判断结果
			if(count == 5) {
				return true;
			}
		}
		
		
//		重置X轴方向的临时变量
		tempX = 1;
//		计算落子的右方棋子个数
		while(col < 0){
			int x = col + tempX;
			MyJLabel chess = chessMap[row][x];
//			上方为null	上方棋子  type 不同
			if(chess == null || chess.getType().equals(myChess.getType()) == false) {
				break;
			}
//			记录个数
			count++;
//			继续向上查找
			tempX++;
//			判断结果
			if(count == 5) {
				return true;
			}
			
		}
		
//		重置X轴方向的临时变量
		tempX = 1;
//		计算落子的左方棋子个数
		while(col > 0){
			int x = col - tempX;
			MyJLabel chess = chessMap[row][x];
//			上方为null	上方棋子  type 不同
			if(chess == null || chess.getType().equals(myChess.getType()) == false) {
				break;
			}
//			记录个数
			count++;
//			继续向上查找
			tempX++;
//			判断结果
			if(count == 5) {
				return true;
			}
		}
		
//		重置Y轴方向的临时变量
		tempY = 1;
//		重置X轴方向的临时变量
		tempX = 1;
//		计算落子的右下方棋子个数
		while(col > 0 && row < 25){
			int y = row + tempY;
			int x = col + tempX;
			MyJLabel chess = chessMap[y][x];
//			上方为null	上方棋子  type 不同
			if(chess == null || chess.getType().equals(myChess.getType()) == false) {
				break;
			}
//			记录个数
			count++;
//			继续向上查找
			tempY++;
			tempX++;
//			判断结果
			if(count == 5) {
				return true;
			}
		}
		
		
//		重置Y轴方向的临时变量
		tempY = 1;
//		重置X轴方向的临时变量
		tempX = 1;
//		计算落子的左上方棋子个数
		while(col < 25 && row > 0){
			int y = row - tempY;
			int x = col - tempX;
			MyJLabel chess = chessMap[y][x];
//			上方为null	上方棋子  type 不同
			if(chess == null || chess.getType().equals(myChess.getType()) == false) {
				break;
			}
//			记录个数
			count++;
//			继续向上查找
			tempY++;
			tempX++;
//			判断结果
			if(count == 5) {
				return true;
			}
		}
	
		System.out.println(myChess.getType()+"\t"+count);
		
		return false;
	}

}

TIP:tempX和tempY可以用同一个变量表示

*public class MyJLabel *

package com.nsg.action;

import javax.swing.JLabel;

public class MyJLabel extends JLabel{
	
	private static final long serialVersionUID = 1L;
	
	private int currentNo;
	private Object content;
	private String type;
	
	
	public MyJLabel(String title, int currentNo) {
		super(title);
		this.currentNo = currentNo;
	}
	public int getCurrentNo() {
		return currentNo;
	}
	public void setCurrentNo(int currentNo) {
		this.currentNo = currentNo;
	}
	public Object getContent() {
		return content;
	}
	public void setContent(Object content) {
		this.content = content;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
		
}

*public class WinInterface *

package com.nsg.action;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class WinInterface {

	JFrame frame;

	public WinInterface() {
		initialize();
	}

	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JButton button = new JButton("\u7EE7\u7EED\u6E38\u620F");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				继续游戏
				new TableFace().frame.setVisible(true);
				WinInterface.this.frame.dispose();
			}
		});
		button.setBounds(147, 72, 129, 42);
		frame.getContentPane().add(button);
		
		JButton button_1 = new JButton("\u9000\u51FA\u6E38\u620F");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				结束程序
				System.exit(0);
			}
		});
		button_1.setBounds(147, 138, 129, 42);
		frame.getContentPane().add(button_1);
		
	}
}

你可能感兴趣的:(Java实现简单的五子棋(swing图形化界面))