?? chessclient.java
字號:
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);
}
}
}
////////////////////////////////////////////////////////////
// 如果點擊的是“創建游戲”按鈕,設定用戶狀態、按鈕狀態,然后與服務器通訊。
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;
}
}
/**
* 鍵盤監聽器,響應“回車按下”事件. 發送消息..
*/
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 ")) {
//之所以是10因為“/yourname ”正好10個字符,后面的是用戶名。。
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 ")) {
//同理,因為/peer 六個字符,所以是6
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) {
//同chessface.chessThread類一樣,這里利用socket讀取會阻塞。。。
message = chessclient.in.readUTF();
acceptMessage(message);
}
} catch (IOException es) {
//讀取異常..讀者可以試著添加相應處理,不如像服務器發回相應的信息提示等,
//從而可提高程序運行可靠性。
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -