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

📄 chessclient.java

📁 Eclipse 编程技术与实例,讲解了Eclipse编辑器的使用方法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				}

			}
		}
		//如果点击的是“创建游戏”按钮,设定用户状态、按钮状态,然后与服务器通讯。
		if (e.getSource() == controlpad.creatGameButton) {
			try {
				//未建立连接时的操作。
				if (!isGameConnected) {
					if (chesspad.connectServer(chesspad.host, chesspad.port)) {
						isGameConnected = true;
						isOnChess = true;
						isServer = true;
						controlpad.creatGameButton.setEnabled(false);
						controlpad.joinGameButton.setEnabled(false);
						controlpad.cancelGameButton.setEnabled(true);
						chesspad.chessthread.sendMessage("/creatgame "
								+ "[inchess]" + chessClientName);
					}
				}
				//建立连接时的操作。
				else {
					isOnChess = true;
					isServer = true;
					controlpad.creatGameButton.setEnabled(false);
					controlpad.joinGameButton.setEnabled(false);
					controlpad.cancelGameButton.setEnabled(true);
					chesspad.chessthread.sendMessage("/creatgame "
							+ "[inchess]" + chessClientName);
				}
			} catch (Exception ec) {
				isGameConnected = false;
				isOnChess = false;
				isServer = false;
				controlpad.creatGameButton.setEnabled(true);
				controlpad.joinGameButton.setEnabled(true);
				controlpad.cancelGameButton.setEnabled(false);
				ec.printStackTrace();
				chatpad.chatLineArea.setText("chesspad.connectServer无法连接 \n"
						+ ec);
			}

		}
		//如果点击的是“取消游戏”按钮,同样要修改按钮状态。
		if (e.getSource() == controlpad.cancelGameButton) {
			//如果棋局正在进行,判定退出游戏的一方输
			if (isOnChess) {
				chesspad.chessthread.sendMessage("/giveup " + chessClientName);
				chesspad.chessVictory(-1 * chesspad.chessColor);
				controlpad.creatGameButton.setEnabled(true);
				controlpad.joinGameButton.setEnabled(true);
				controlpad.cancelGameButton.setEnabled(false);
				chesspad.statusText.setText("请建立游戏或者加入游戏");
			}
			if (!isOnChess) {
				controlpad.creatGameButton.setEnabled(true);
				controlpad.joinGameButton.setEnabled(true);
				controlpad.cancelGameButton.setEnabled(false);
				chesspad.statusText.setText("请建立游戏或者加入游戏");
			}
			isClient = isServer = false;
		}
		//如果点击的是“改变颜色”按钮,则弹出“颜色选择”对话框供用户选择界面颜色。
		if (e.getSource() == controlpad.colorChangeButton) {
			JColorChooser nextColor = new JColorChooser();
			Color selectedColor = JColorChooser.showDialog(this, "选择背景颜色",
					Color.BLACK);
			chesspad.setBackground(selectedColor);
			inputpad.setBackground(selectedColor);
			controlpad.setBackground(selectedColor);
			chesspad.statusLabel.setBackground(selectedColor);
			inputpad.chatLabel.setBackground(selectedColor);
			controlpad.IPlabel.setBackground(selectedColor);
			westPanel.setBackground(selectedColor);
			centerPanel.setBackground(selectedColor);
			southPanel.setBackground(selectedColor);
		}

	}

	/**
	 * 键盘监听器,响应“回车按下”事件.
	 */
	public void keyPressed(KeyEvent e) {
		TextField inputWords = (TextField) e.getSource();
		//如果选择向所有人发消息,则将所发消息直接发给服务器
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			if (inputpad.userChoice.getSelectedItem().equals("所有人")) {
				try {
					out.writeUTF(inputWords.getText());
					inputWords.setText("");
				} catch (Exception ea) {
					chatpad.chatLineArea
							.setText("chessClient:KeyPressed无法连接,建议重新连接 \n");
					userpad.userList.removeAll();
					inputpad.userChoice.removeAll();
					inputWords.setText("");
					controlpad.connectButton.setEnabled(true);
				}
			}
			//如果选择向一个人发消息,则将所发消息封装成一定格式发给服务器
			else {
				try {
					out.writeUTF("/" + inputpad.userChoice.getSelectedItem()
							+ " " + inputWords.getText());
					inputWords.setText("");
				} catch (Exception ea) {
					chatpad.chatLineArea
							.setText("chessClient:KeyPressed无法连接,建议重新连接 \n");
					userpad.userList.removeAll();
					inputpad.userChoice.removeAll();
					inputWords.setText("");
					controlpad.connectButton.setEnabled(true);
				}
			}
		}

	}

	public void keyTyped(KeyEvent e) {
	}

	public void keyReleased(KeyEvent e) {
	}

	public static void main(String args[]) {
		chessClient chessClient = new chessClient();
	}
}
/**
 * 客户端线程
 */

class clientThread extends Thread {
	chessClient chessclient;

	clientThread(chessClient chessclient) {
		this.chessclient = chessclient;
	}

	/**
	 * 客户端线程对接收到的信息进行处理的函数
	 */
	public void acceptMessage(String recMessage) {
		if (recMessage.startsWith("/userlist ")) {
			//如果接收到的信息以"/userlist "开头,将其后的用户名提取出来,添加到
			//输入信息Panel左边的用户列表中。
			StringTokenizer userToken = new StringTokenizer(recMessage, " ");
			int userNumber = 0;

			chessclient.userpad.userList.removeAll();
			chessclient.inputpad.userChoice.removeAll();
			chessclient.inputpad.userChoice.addItem("所有人");
			while (userToken.hasMoreTokens()) {
				String user = (String) userToken.nextToken(" ");
				if (userNumber > 0 && !user.startsWith("[inchess]")) {
					chessclient.userpad.userList.add(user);
					chessclient.inputpad.userChoice.addItem(user);
				}

				userNumber++;
			}
			chessclient.inputpad.userChoice.select("所有人");
		}
		//如果如果接收到的信息以"/yourname "开头,将用户名显示在客户端对话框标题栏。
		else if (recMessage.startsWith("/yourname ")) {
			chessclient.chessClientName = recMessage.substring(10);
			chessclient.setTitle("五子棋客户端 " + "当前用户名:"
					+ chessclient.chessClientName);
		}
		//如果如果接收到的信息以"/reject"开头,在状态栏显示拒绝加入游戏。
		else if (recMessage.equals("/reject")) {
			try {
				chessclient.chesspad.statusText.setText("不能加入游戏");
				chessclient.controlpad.cancelGameButton.setEnabled(false);
				chessclient.controlpad.joinGameButton.setEnabled(true);
				chessclient.controlpad.creatGameButton.setEnabled(true);
			} catch (Exception ef) {
				chessclient.chatpad.chatLineArea
						.setText("chessclient.chesspad.chessSocket.close无法关闭");
			}
			chessclient.controlpad.joinGameButton.setEnabled(true);
		}
		//如果如果接收到的信息以"/peer"开头,则记下对方的名字,然后进入等待状态
		else if (recMessage.startsWith("/peer ")) {
			chessclient.chesspad.chessPeerName = recMessage.substring(6);
			if (chessclient.isServer) {
				chessclient.chesspad.chessColor = 1;
				chessclient.chesspad.isMouseEnabled = true;
				chessclient.chesspad.statusText.setText("请黑棋下子");
			} else if (chessclient.isClient) {
				chessclient.chesspad.chessColor = -1;
				chessclient.chesspad.statusText.setText("已加入游戏,等待对方下子...");
			}

		} else if (recMessage.equals("/youwin")) {
			chessclient.isOnChess = false;
			chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor);
			chessclient.chesspad.statusText.setText("对方退出,请点放弃游戏退出连接");
			chessclient.chesspad.isMouseEnabled = false;
		} else if (recMessage.equals("/OK")) {
			chessclient.chesspad.statusText.setText("创建游戏成功,等待别人加入...");
		} else if (recMessage.equals("/error")) {
			chessclient.chatpad.chatLineArea.append("传输错误:请退出程序,重新加入 \n");
		} else {
			chessclient.chatpad.chatLineArea.append(recMessage + "\n");
			chessclient.chatpad.chatLineArea
					.setCaretPosition(chessclient.chatpad.chatLineArea
							.getText().length());
		}
	}

	public void run() {
		String message = "";
		try {
			while (true) {
				message = chessclient.in.readUTF();
				acceptMessage(message);
			}
		} catch (IOException es) {
		}
	}

}

⌨️ 快捷键说明

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