?? chessclient.java
字號:
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
* 五子棋客戶端框架,實現了動作監聽器和鍵盤監聽器
*/
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 southPanel = new Panel();
Panel centerPanel = new Panel();
Panel westPanel = new Panel();
/*
* 五子棋客戶端框架的構造函數。用來初始化一些對象、布局和為按鈕添加監聽器。
*/
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));
//添加窗口監聽器,當窗口關閉時,關閉用于通訊的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();
}
/**
* 和服務器建立連接并通信的函數。
*/
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:無法連接,建議重新啟動程序 \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);
}
}
//如果已建立連接,省去建立連接的操作。
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);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -