?? chessclient.java
字號:
package client;
import chessface.chatPad;
import chessface.inputPad;
import chessface.userPad;
import chessface.chessPad;
import chessface.controlPad;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
/*
* 五子棋客戶端框架,實現了動作監聽器和鍵盤監聽器
*/
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; // 是否建立游戲的主機
boolean isClient = false; // 是否加入游戲的客戶端
Panel northPanel = new Panel();
Panel centerPanel = new Panel();
Panel eastPanel = new Panel();
/*
* 五子棋客戶端框架的構造函數。用來初始化一些對象、布局和為按鈕添加監聽器。
*/
public chessClient() {
// 窗口標題
super("五子棋客戶端");
// 設置布局
setLayout(new BorderLayout());
// 獲取服務器ip地址
host = controlpad.inputIP.getText();
// 右側的Panel,向里面添加用戶列表和聊天信息列表的Panel
// 設置eastPanel布局
eastPanel.setLayout(new BorderLayout());
// 在其上添加用于列表
eastPanel.add(userpad, BorderLayout.NORTH);
// 在其上添加聊天信息窗口
eastPanel.add(chatpad, BorderLayout.CENTER);
// 設定背景色
eastPanel.setBackground(new Color(204, 204, 204));
// 對編輯輸出信息窗口添加事件監聽器..
inputpad.inputWords.addKeyListener(this);
// 設置棋盤Panel所需的服務器地址
chesspad.host = controlpad.inputIP.getText();
// 向centerPanel中添加棋盤和用戶輸入消息panel,并設定背景顏色
centerPanel.add(chesspad, BorderLayout.CENTER);
centerPanel.add(inputpad, BorderLayout.SOUTH);
centerPanel.setBackground(new Color(204, 204, 204));
// 為controlpad中的按鈕們添加事件監聽...
controlpad.connectButton.addActionListener(this);
controlpad.creatGameButton.addActionListener(this);
controlpad.joinGameButton.addActionListener(this);
controlpad.cancelGameButton.addActionListener(this);
controlpad.exitGameButton.addActionListener(this);
// 初始設定這幾個按鈕為非激活狀態..
controlpad.creatGameButton.setEnabled(false);
controlpad.joinGameButton.setEnabled(false);
controlpad.cancelGameButton.setEnabled(false);
// 添加controlpad到northPanel---最上端的panel
northPanel.add(controlpad, BorderLayout.CENTER);
northPanel.setBackground(new Color(204, 204, 204));
// 添加窗口監聽器,當窗口關閉時,關閉用于通訊的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);
}
});
// 添加這些panel到frame,組成最終窗口
add(eastPanel, BorderLayout.EAST);
add(centerPanel, BorderLayout.CENTER);
add(northPanel, BorderLayout.NORTH);
// 設置大小等
setSize(670, 560);
setVisible(true);
setResizable(true);
validate();
}
/**
* 和服務器建立連接并通信的函數。
* @return true 如果連接成功, false 如果連接失敗..
*/
public boolean connectServer(String serverIP, int serverPort) throws Exception {
try {
// System.out.println("in chessClient#connectServer");
//建立聊天socket
chatSocket = new Socket(serverIP, serverPort);
//由socket得到輸入輸出流.
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:無法連接,建議重新啟動程序 \n");
}
return false;
}
/**
* 動作監聽器,響應按鈕點擊動作。
*/
public void actionPerformed(ActionEvent e) {
////////////////////////////////////////////////////
// 如果點擊的是“連接主機”按鈕,則用獲取的服務器主機名連接服務器。
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("連接成功,請創建游戲或加入游戲");
}
} catch (Exception ei) { //連接失敗
chatpad.chatLineArea.setText("controlpad.connectButton:無法連接,建議重新啟動程序 \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);
}
///////////////////////////////////////////////////////
// 如果點擊的是“加入游戲”按鈕,則先判斷選定的加入的目標是否有效。
// 如果選定的目標為空或正在下棋或為其本身,則認為目標無效。
if (e.getSource() == controlpad.joinGameButton) {
// 得到選擇的用戶
String selectedUser = userpad.userList.getSelectedItem();
if (selectedUser == null || selectedUser.startsWith("[inchess]") || selectedUser.equals(chessClientName)) {
chesspad.statusText.setText("必須先選定一個有效用戶");
} else {
try {
// 如果未建立與服務器的連接,創建連接,設定用戶的當前狀態。
// 此外還要對按鈕作一些處理,將“創建連接”按鈕和“加入游戲按鈕”設為不可用。
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);
}
}
// 如果已建立連接,省去建立連接的操作。
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -