⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chessclient.java

📁 Eclipse编程技术与实例一书的附CD-ROM光盘
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.fivechess.client;

import com.fivechess.chessface.chatPad;
import com.fivechess.chessface.inputPad;
import com.fivechess.chessface.userPad;
import com.fivechess.chessface.chessPad;
import com.fivechess.chessface.controlPad;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.JColorChooser;

/**
 * @author wufenghanren 
 * 五子棋客户端框架,实现了动作监听器和键盘监听器
 */

public class chessClient extends Frame implements ActionListener, KeyListener {
	userPad userpad = new userPad();//用户列表Panel

	chatPad chatpad = new chatPad();//聊天信息Panel

	controlPad controlpad = new controlPad();//控制Panel

	chessPad chesspad = new chessPad();//棋盘Panel

	inputPad inputpad = new inputPad();//信息输入Panel

	Socket chatSocket;

	DataInputStream in;

	DataOutputStream out;

	String chessClientName = null;

	String host = null;

	int port = 4331;

	boolean isOnChat = false; //是否在聊天

	boolean isOnChess = false; //是否在下棋

	boolean isGameConnected = false; //是否下棋的客户端连接

	boolean isServer = false; //是否建立游戏的主机

	boolean isClient = false; //是否加入游戏的客户端

	Panel southPanel = new Panel();

	Panel centerPanel = new Panel();

	Panel westPanel = new Panel();

	/*
	 * 五子棋客户端框架的构造函数。用来初始化一些对象、布局和为按钮添加监听器。
	 */
	public chessClient() {
		super("五子棋客户端");
		setLayout(new BorderLayout());
		host = controlpad.inputIP.getText();

		westPanel.setLayout(new BorderLayout());
		westPanel.add(userpad, BorderLayout.NORTH);
		westPanel.add(chatpad, BorderLayout.CENTER);
		westPanel.setBackground(new Color(204, 204, 204));

		inputpad.inputWords.addKeyListener(this);
		chesspad.host = controlpad.inputIP.getText();

		centerPanel.add(chesspad, BorderLayout.CENTER);
		centerPanel.add(inputpad, BorderLayout.SOUTH);
		centerPanel.setBackground(new Color(204, 204, 204));

		controlpad.connectButton.addActionListener(this);
		controlpad.creatGameButton.addActionListener(this);
		controlpad.joinGameButton.addActionListener(this);
		controlpad.cancelGameButton.addActionListener(this);
		controlpad.colorChangeButton.addActionListener(this);
		controlpad.exitGameButton.addActionListener(this);

		controlpad.creatGameButton.setEnabled(false);
		controlpad.joinGameButton.setEnabled(false);
		controlpad.cancelGameButton.setEnabled(false);

		southPanel.add(controlpad, BorderLayout.CENTER);
		southPanel.setBackground(new Color(204, 204, 204));
		//添加窗口监听器,当窗口关闭时,关闭用于通讯的Socket。
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				if (isOnChat) {
					try {
						chatSocket.close();
					} catch (Exception ed) {
					}
				}
				if (isOnChess || isGameConnected) {
					try {
						chesspad.chessSocket.close();
					} catch (Exception ee) {
					}
				}
				System.exit(0);
			}
		});

		add(westPanel, BorderLayout.WEST);
		add(centerPanel, BorderLayout.CENTER);
		add(southPanel, BorderLayout.SOUTH);

		setSize(670, 548);
		setVisible(true);
		setResizable(true);
		validate();
	}

	/**
	 * 和服务器建立连接并通信的函数。
	 */
	public boolean connectServer(String serverIP, int serverPort)
			throws Exception {
		try {
			chatSocket = new Socket(serverIP, serverPort);
			in = new DataInputStream(chatSocket.getInputStream());
			out = new DataOutputStream(chatSocket.getOutputStream());

			clientThread clientthread = new clientThread(this);
			clientthread.start();
			isOnChat = true;
			return true;
		} catch (IOException ex) {
			chatpad.chatLineArea
					.setText("chessClient:connectServer:无法连接,建议重新启动程序 \n");
		}
		return false;
	}

	/**
	 * 动作监听器,响应按钮点击动作。
	 */
	public void actionPerformed(ActionEvent e) {
		//如果点击的是“连接主机”按钮,则用获取的服务器主机名连接服务器。
		if (e.getSource() == controlpad.connectButton) {
			host = chesspad.host = controlpad.inputIP.getText();
			try {
				if (connectServer(host, port)) {
					chatpad.chatLineArea.setText("");
					controlpad.connectButton.setEnabled(false);
					controlpad.creatGameButton.setEnabled(true);
					controlpad.joinGameButton.setEnabled(true);
					chesspad.statusText.setText("连接成功,请创建游戏或加入游戏");
				}

			} catch (Exception ei) {
				chatpad.chatLineArea
						.setText("controlpad.connectButton:无法连接,建议重新启动程序 \n");
			}
		}
		//		如果点击的是“关闭程序”按钮,则关闭正在进行通信的Socekt并退出游戏。
		if (e.getSource() == controlpad.exitGameButton) {
			if (isOnChat) {
				try {
					chatSocket.close();
				} catch (Exception ed) {
				}
			}
			if (isOnChess || isGameConnected) {
				try {
					chesspad.chessSocket.close();
				} catch (Exception ee) {
				}
			}
			System.exit(0);

		}
		//如果点击的是“加入游戏”按钮,则先判断选定的加入的目标是否有效。
		//如果选定的目标为空或正在下棋或为其本身,则认为目标无效。
		if (e.getSource() == controlpad.joinGameButton) {
			String selectedUser = userpad.userList.getSelectedItem();
			if (selectedUser == null || selectedUser.startsWith("[inchess]")
					|| selectedUser.equals(chessClientName)) {
				chesspad.statusText.setText("必须先选定一个有效用户");
			} else {
				try {
					//如果未建立与服务器的连接,创建连接,设定用户的当前状态。
					//此外还要对按钮作一些处理,将“创建连接”按钮和“加入游戏按钮”设为不可用。
					if (!isGameConnected) {
						if (chesspad
								.connectServer(chesspad.host, chesspad.port)) {
							isGameConnected = true;
							isOnChess = true;
							isClient = true;
							controlpad.creatGameButton.setEnabled(false);
							controlpad.joinGameButton.setEnabled(false);
							controlpad.cancelGameButton.setEnabled(true);
							chesspad.chessthread.sendMessage("/joingame "
									+ userpad.userList.getSelectedItem() + " "
									+ chessClientName);
						}
					}
					//如果已建立连接,省去建立连接的操作。
					else {
						isOnChess = true;
						isClient = true;
						controlpad.creatGameButton.setEnabled(false);
						controlpad.joinGameButton.setEnabled(false);
						controlpad.cancelGameButton.setEnabled(true);
						chesspad.chessthread.sendMessage("/joingame "
								+ userpad.userList.getSelectedItem() + " "
								+ chessClientName);
					}

				} catch (Exception ee) {
					isGameConnected = false;
					isOnChess = false;
					isClient = false;
					controlpad.creatGameButton.setEnabled(true);
					controlpad.joinGameButton.setEnabled(true);
					controlpad.cancelGameButton.setEnabled(false);
					chatpad.chatLineArea
							.setText("chesspad.connectServer无法连接 \n" + ee);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -