?? chessclient.java
字號(hào):
}
}
}
//如果點(diǎn)擊的是“創(chuàng)建游戲”按鈕,設(shè)定用戶(hù)狀態(tài)、按鈕狀態(tài),然后與服務(wù)器通訊。
if (e.getSource() == controlpad.creatGameButton) {
try {
//未建立連接時(shí)的操作。
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);
}
}
//建立連接時(shí)的操作。
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無(wú)法連接 \n"
+ ec);
}
}
//如果點(diǎn)擊的是“取消游戲”按鈕,同樣要修改按鈕狀態(tài)。
if (e.getSource() == controlpad.cancelGameButton) {
//如果棋局正在進(jìn)行,判定退出游戲的一方輸
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("請(qǐng)建立游戲或者加入游戲");
}
if (!isOnChess) {
controlpad.creatGameButton.setEnabled(true);
controlpad.joinGameButton.setEnabled(true);
controlpad.cancelGameButton.setEnabled(false);
chesspad.statusText.setText("請(qǐng)建立游戲或者加入游戲");
}
isClient = isServer = false;
}
//如果點(diǎn)擊的是“改變顏色”按鈕,則彈出“顏色選擇”對(duì)話(huà)框供用戶(hù)選擇界面顏色。
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);
}
}
/**
* 鍵盤(pán)監(jiān)聽(tīng)器,響應(yīng)“回車(chē)按下”事件.
*/
public void keyPressed(KeyEvent e) {
TextField inputWords = (TextField) e.getSource();
//如果選擇向所有人發(fā)消息,則將所發(fā)消息直接發(fā)給服務(wù)器
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無(wú)法連接,建議重新連接 \n");
userpad.userList.removeAll();
inputpad.userChoice.removeAll();
inputWords.setText("");
controlpad.connectButton.setEnabled(true);
}
}
//如果選擇向一個(gè)人發(fā)消息,則將所發(fā)消息封裝成一定格式發(fā)給服務(wù)器
else {
try {
out.writeUTF("/" + inputpad.userChoice.getSelectedItem()
+ " " + inputWords.getText());
inputWords.setText("");
} catch (Exception ea) {
chatpad.chatLineArea
.setText("chessClient:KeyPressed無(wú)法連接,建議重新連接 \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();
}
}
/**
* 客戶(hù)端線(xiàn)程
*/
class clientThread extends Thread {
chessClient chessclient;
clientThread(chessClient chessclient) {
this.chessclient = chessclient;
}
/**
* 客戶(hù)端線(xiàn)程對(duì)接收到的信息進(jìn)行處理的函數(shù)
*/
public void acceptMessage(String recMessage) {
if (recMessage.startsWith("/userlist ")) {
//如果接收到的信息以"/userlist "開(kāi)頭,將其后的用戶(hù)名提取出來(lái),添加到
//輸入信息Panel左邊的用戶(hù)列表中。
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 "開(kāi)頭,將用戶(hù)名顯示在客戶(hù)端對(duì)話(huà)框標(biāo)題欄。
else if (recMessage.startsWith("/yourname ")) {
chessclient.chessClientName = recMessage.substring(10);
chessclient.setTitle("五子棋客戶(hù)端 " + "當(dāng)前用戶(hù)名:"
+ chessclient.chessClientName);
}
//如果如果接收到的信息以"/reject"開(kāi)頭,在狀態(tài)欄顯示拒絕加入游戲。
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無(wú)法關(guān)閉");
}
chessclient.controlpad.joinGameButton.setEnabled(true);
}
//如果如果接收到的信息以"/peer"開(kāi)頭,則記下對(duì)方的名字,然后進(jìn)入等待狀態(tài)
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("請(qǐng)黑棋下子");
} else if (chessclient.isClient) {
chessclient.chesspad.chessColor = -1;
chessclient.chesspad.statusText.setText("已加入游戲,等待對(duì)方下子...");
}
} else if (recMessage.equals("/youwin")) {
chessclient.isOnChess = false;
chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor);
chessclient.chesspad.statusText.setText("對(duì)方退出,請(qǐng)點(diǎn)放棄游戲退出連接");
chessclient.chesspad.isMouseEnabled = false;
} else if (recMessage.equals("/OK")) {
chessclient.chesspad.statusText.setText("創(chuàng)建游戲成功,等待別人加入...");
} else if (recMessage.equals("/error")) {
chessclient.chatpad.chatLineArea.append("傳輸錯(cuò)誤:請(qǐng)退出程序,重新加入 \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) {
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -