?? chessclient.java
字號:
}
}
}
//如果點擊的是“創建游戲”按鈕,設定用戶狀態、按鈕狀態,然后與服務器通訊。
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 + -