?? chessclient.java
字號(hào):
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
* 五子棋客戶端框架,實(shí)現(xiàn)了動(dòng)作監(jiān)聽器和鍵盤監(jiān)聽器
*/
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; //是否建立游戲的主機(jī)
boolean isClient = false; //是否加入游戲的客戶端
Panel southPanel = new Panel();
Panel centerPanel = new Panel();
Panel westPanel = new Panel();
/*
* 五子棋客戶端框架的構(gòu)造函數(shù)。用來(lái)初始化一些對(duì)象、布局和為按鈕添加監(jiān)聽器。
*/
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));
//添加窗口監(jiān)聽器,當(dāng)窗口關(guān)閉時(shí),關(guān)閉用于通訊的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();
}
/**
* 和服務(wù)器建立連接并通信的函數(shù)。
*/
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:無(wú)法連接,建議重新啟動(dòng)程序 \n");
}
return false;
}
/**
* 動(dòng)作監(jiān)聽器,響應(yīng)按鈕點(diǎn)擊動(dòng)作。
*/
public void actionPerformed(ActionEvent e) {
//如果點(diǎn)擊的是“連接主機(jī)”按鈕,則用獲取的服務(wù)器主機(jī)名連接服務(wù)器。
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("連接成功,請(qǐng)創(chuàng)建游戲或加入游戲");
}
} catch (Exception ei) {
chatpad.chatLineArea
.setText("controlpad.connectButton:無(wú)法連接,建議重新啟動(dòng)程序 \n");
}
}
// 如果點(diǎn)擊的是“關(guān)閉程序”按鈕,則關(guān)閉正在進(jì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);
}
//如果點(diǎn)擊的是“加入游戲”按鈕,則先判斷選定的加入的目標(biāo)是否有效。
//如果選定的目標(biāo)為空或正在下棋或?yàn)槠浔旧恚瑒t認(rèn)為目標(biāo)無(wú)效。
if (e.getSource() == controlpad.joinGameButton) {
String selectedUser = userpad.userList.getSelectedItem();
if (selectedUser == null || selectedUser.startsWith("[inchess]")
|| selectedUser.equals(chessClientName)) {
chesspad.statusText.setText("必須先選定一個(gè)有效用戶");
} else {
try {
//如果未建立與服務(wù)器的連接,創(chuàng)建連接,設(shè)定用戶的當(dāng)前狀態(tài)。
//此外還要對(duì)按鈕作一些處理,將“創(chuàng)建連接”按鈕和“加入游戲按鈕”設(shè)為不可用。
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無(wú)法連接 \n" + ee);
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -