ClientAgentThread.java
~~~
import javax.swing.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
public class ClientAgentThread extends Thread {
XiangQi father;//聲明XiangQi的引用
boolean flag=true;//控制線程的標志位
DataInputStream din;//聲明數據輸入輸出流的引用
DataOutputStream dout;
String tiaoZhanZhe=null;//用于記錄正在挑戰的對手
public ClientAgentThread(XiangQi father)
{
this.father=father;
try
{
din=new DataInputStream(father.sc.getInputStream());//創建數據輸入輸出流
dout=new DataOutputStream(father.sc.getOutputStream());
String name=father.jtfNickName.getText().trim();//獲得昵稱
dout.writeUTF("<#NICK_NAME#>"+name);//發送昵稱到服務器
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run(){
while(flag){
try{
String msg=din.readUTF().trim();//獲得服務器發來的信息
if(msg.startsWith("<#NAME_CHONGMING#>")){//收到重名的信息
this.name_chongming();
}
else if(msg.startsWith("<#NICK_LIST#>")){//收到昵稱列表
this.nick_list(msg);
}
else if(msg.startsWith("<#SERVER_DOWN#>")){//當收到服務器離開的信息
this.server_down();
}
else if(msg.startsWith("<#TIAO_ZHAN#>")){//當收到挑戰的信息
this.tiao_zhan(msg);
}
else if(msg.startsWith("<#TONG_YI#>")){
this.tong_yi();//當該用戶收到對方接受挑戰的信息時
}
else if(msg.startsWith("<#BUTONG_YI#>")){
this.butong_yi();//當該用戶收到對方拒絕挑戰的信息時
}
else if(msg.startsWith("<#BUSY#>")){//當收到對方忙的信息時
this.busy();
}
else if(msg.startsWith("<#MOVE#>")){//當收到走棋的信息時
this.move(msg);
}
else if(msg.startsWith("<#RENSHU#>")){//當收到認輸的信息時
this.renshu();
}
}
catch(Exception e){e.printStackTrace();}
}
}
public void name_chongming(){
try{
JOptionPane.showMessageDialog(this.father,"該玩家名稱已經被占用,請重新填寫!",
"錯誤",JOptionPane.ERROR_MESSAGE);//給出重名的提示信息
din.close();//關閉數據輸入流
dout.close();//關閉數據輸出流
this.father.jtfHost.setEnabled(!false);//將用于輸入主機名的文本框設為可用
this.father.jtfPort.setEnabled(!false);//將用于輸入端口號的文本框設為可用
this.father.jtfNickName.setEnabled(!false);//將用于輸入昵稱的文本框設為可用
this.father.jbConnect.setEnabled(!false);//將"連接"按鈕設為可用
this.father.jbDisconnect.setEnabled(!true);//將"斷開"按鈕設為不可用
this.father.jbChallenge.setEnabled(!true);//將"挑戰"按鈕設為不可用
this.father.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用
this.father.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用
this.father.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用
father.sc.close();//關閉Socket
father.sc=null;
father.cat=null;
flag=false;//終止該客戶端代理線程
}
catch(IOException e){e.printStackTrace();}
}
public void nick_list(String msg){
String s=msg.substring(13);//分解并得到有用信息
String[] na=s.split("\\|");
Vector v=new Vector();//創建Vector對象
for(int i=0;i<na.length;i++){
if(na[i].trim().length()!=0&&(!na[i].trim().equals(father.jtfNickName.getText().trim()))){
v.add(na[i]);//將昵稱全部添加到Vector中
}
}
father.jcbNickList.setModel(new DefaultComboBoxModel(v));//設置下拉列表的值
}
public void server_down(){
this.father.jtfHost.setEnabled(!false);//將用于輸入主機名的文本框設為可用
this.father.jtfPort.setEnabled(!false);;//將用于輸入端口號的文本框設為可用
this.father.jtfNickName.setEnabled(!false);//將用于輸入昵稱的文本框設為可用
this.father.jbConnect.setEnabled(!false);//將"連接"按鈕設為可用
this.father.jbDisconnect.setEnabled(!true);//將"斷開"按鈕設為不可用
this.father.jbChallenge.setEnabled(!true);//將"挑戰"按鈕設為不可用
this.father.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用
this.father.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用
this.father.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用
this.flag=false;//終止該客戶端代理線程
father.cat=null;
JOptionPane.showMessageDialog(this.father,"服務器停止!!!","提示",
JOptionPane.INFORMATION_MESSAGE);//給出服務器離開的提示信息
}
public void tiao_zhan(String msg){
try{
String name=msg.substring(13);//獲得挑戰者的昵稱
if(this.tiaoZhanZhe==null){//如果玩家空閑
tiaoZhanZhe=msg.substring(13);//將tiaoZhanZhe的值賦為挑戰者的昵稱
this.father.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用
this.father.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用
this.father.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用
this.father.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用
this.father.jbDisconnect.setEnabled(!true);//將"斷開"按鈕設為不可用
this.father.jbChallenge.setEnabled(!true);//將"挑戰"按鈕設為不可用
this.father.jbYChallenge.setEnabled(!false);//將"接受挑戰"按鈕設為可用
this.father.jbNChallenge.setEnabled(!false);//將"拒絕挑戰"按鈕設為可用
this.father.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用
JOptionPane.showMessageDialog(this.father,tiaoZhanZhe+"向你挑戰!!!",
"提示",JOptionPane.INFORMATION_MESSAGE);//給出挑戰信息
}
else{//如果該玩家忙碌中 ,則返回一個<#BUSY#>開頭的信息
this.dout.writeUTF("<#BUSY#>"+name);
}
}
catch(IOException e){e.printStackTrace();}
}
public void tong_yi(){
this.father.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用
this.father.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用
this.father.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用
this.father.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用
this.father.jbDisconnect.setEnabled(!true);//將"斷開"按鈕設為不可用
this.father.jbChallenge.setEnabled(!true);//將"挑戰"按鈕設為不可用
this.father.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用
this.father.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用
this.father.jbFail.setEnabled(!false);//將"認輸"按鈕設為不可用
JOptionPane.showMessageDialog(this.father,"對方接受您的挑戰!您先走棋(紅棋)",
"提示",JOptionPane.INFORMATION_MESSAGE);
}
public void butong_yi(){
this.father.caiPan=false;//將caiPan設為false
this.father.color=0;//將color設為0
this.father.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用
this.father.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用
this.father.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用
this.father.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用
this.father.jbDisconnect.setEnabled(true);//將"斷開"按鈕設為可用
this.father.jbChallenge.setEnabled(true);//將"挑戰"按鈕設為可用
this.father.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用
this.father.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用
this.father.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用
JOptionPane.showMessageDialog(this.father,"對方拒絕您的挑戰!","提示",
JOptionPane.INFORMATION_MESSAGE);//給出對方拒絕挑戰的提示信息
this.tiaoZhanZhe=null;
}
public void busy(){
this.father.caiPan=false;//將caiPan設為false
this.father.color=0;//將color設為0
this.father.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用
this.father.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用
this.father.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用
this.father.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用
this.father.jbDisconnect.setEnabled(true);//將"斷開"按鈕設為可用
this.father.jbChallenge.setEnabled(true);//將"挑戰"按鈕設為可用
this.father.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用
this.father.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用
this.father.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用
JOptionPane.showMessageDialog(this.father,"對方忙碌中!!!","提示",
JOptionPane.INFORMATION_MESSAGE);//給出對方忙碌的提示信息
this.tiaoZhanZhe=null;
}
public void move(String msg){
int length=msg.length();
int startI=Integer.parseInt(msg.substring(length-4,length-3));//獲得棋子的原始位置
int startJ=Integer.parseInt(msg.substring(length-3,length-2));
int endI=Integer.parseInt(msg.substring(length-2,length-1));//獲得走后的位置
int endJ=Integer.parseInt(msg.substring(length-1));
//this.father.jpz.move(startI,startJ,endI,endJ);//調用方法走棋
this.father.caiPan=true;//將canPan設為true
}
public void renshu(){
JOptionPane.showMessageDialog(this.father,"恭喜你,你獲勝,對方認輸","提示",
JOptionPane.INFORMATION_MESSAGE);//給出獲勝信息
this.tiaoZhanZhe=null;//將挑戰者設為空
this.father.color=0;//將color設為0
this.father.caiPan=false;//將caiPan設為false
this.father.next();//進入下一盤
this.father.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用
this.father.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用
this.father.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用
this.father.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用
this.father.jbDisconnect.setEnabled(true);//將"斷開"按鈕設為可用
this.father.jbChallenge.setEnabled(true);//將"挑戰"按鈕設為可用
this.father.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用
this.father.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用
this.father.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用
}
}
~~~
將服務器線程啟動
ServerThread.java
~~~
import java.net.ServerSocket;
import java.net.Socket;
public class ServerThread extends Thread{
Server father; //聲明Server的引用
ServerSocket ss;//聲明ServerSocket的引用
boolean flag=true;//z操作線程的生存期
public ServerThread(Server father)
{//構造器
this.father=father;
ss=father.ss;
}
public void run()
{
while(flag)
{
try
{
Socket sc=ss.accept();//等待客戶端連接
ServerAgentThread sat=new ServerAgentThread(father,sc);
sat.start();//創建并啟動服務器代理線程
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
~~~
在象棋類中創建客戶端線程,并在控件、邏輯中書寫相關內容
XiangQi.java
~~~
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.Socket;
public class XiangQi extends JFrame implements ActionListener {
public static final Color bgColor=new Color(245,250,160);//棋盤的背景色
public static final Color focusbg=new Color(242,242,242);//棋子選中后的背景色
public static final Color focuschar=new Color(96,95,91);//棋子選中后的字符顏色
public static final Color color1=new Color(249,183,173);//紅方的顏色
public static final Color color2=Color.white;//白方的顏色
JLabel jlHost=new JLabel("主機名");//創建提示輸入主機名的標簽
JLabel jlPort=new JLabel("端口號");////創建提示輸入端口號標簽
JLabel jlNickName=new JLabel("昵 稱");//創建提示輸入昵稱的標簽
JTextField jtfHost=new JTextField("127.0.0.1");//創建輸入主機名的文本框,默認值是"127.0.0.1"
JTextField jtfPort=new JTextField("9999");//創建輸入端口號的文本框,默認值是9999
JTextField jtfNickName=new JTextField("Play1");//創建輸入昵稱的文本框,默認值是Play1
JButton jbConnect=new JButton("連 接");//創建"連接"按鈕
JButton jbDisconnect=new JButton("斷 開");//創建"斷開"按鈕
JButton jbFail=new JButton("認 輸");//創建"認輸"按鈕
JButton jbChallenge=new JButton("挑 戰");//創建"挑戰"按鈕
JComboBox jcbNickList=new JComboBox();//創建存放當前用戶的下拉列表框
JButton jbYChallenge=new JButton("接受挑戰");//創建"接受挑戰"按鈕
JButton jbNChallenge=new JButton("拒絕挑戰");//創建"拒絕挑戰"按鈕
int width=60;//設置棋盤兩線之間的距離//單位是像素
QiZi[][] qiZi=new QiZi[9][10];//創建棋子數組
//QiPan jpz=new QiPan(qiZi,width,this);//創建棋盤
JPanel jpz=new JPanel();//創建一個JPanel,暫時代替棋盤
JPanel jpy=new JPanel();//創建一個JPanel
JSplitPane jsp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jpz,jpy);//創建一個JSplitPane
boolean caiPan=false;//可否走棋的標志位
int color=0;//0 代表紅棋,1代表白棋
Socket sc;//聲明Socket引用
ClientAgentThread cat;//聲明客戶端代理線程的引用
public XiangQi()
{
this.initialComponent();//初始化控件
this.addListener();//為相應控件注冊事件監聽器
this.initialState();//初始化狀態
this.initialQiZi();//初始化棋子
this.initialFrame();//初始化窗體
}
public void initialComponent()//初始化控件
{
jpy.setLayout(null);//設為空布局
this.jlHost.setBounds(10,10,50,20);//前兩個參數是大小,后面兩個是位置
jpy.add(this.jlHost);//添加"主機名"標簽
this.jtfHost.setBounds(70,10,80,20);
jpy.add(this.jtfHost);//添加用于輸入主機名的文本框
this.jlPort.setBounds(10,40,50,20);
jpy.add(this.jlPort);//添加"端口號"標簽
this.jtfPort.setBounds(70,40,80,20);
jpy.add(this.jtfPort);//添加用于輸入端口號的文本框
this.jlNickName.setBounds(10,70,50,20);
jpy.add(this.jlNickName);//添加"昵稱"標簽
this.jtfNickName.setBounds(70,70,80,20);
jpy.add(this.jtfNickName);//添加用于輸入昵稱的文本框
this.jbConnect.setBounds(10,100,80,20);
jpy.add(this.jbConnect);//添加"連接"按鈕
this.jbDisconnect.setBounds(100,100,80,20);
jpy.add(this.jbDisconnect);//添加"斷開"按鈕
this.jcbNickList.setBounds(20,130,130,20);
jpy.add(this.jcbNickList);//添加用于顯示當前用戶的下拉列表框
this.jbChallenge.setBounds(10,160,80,20);
jpy.add(this.jbChallenge);//添加"挑戰"按鈕
this.jbFail.setBounds(100,160,80,20);
jpy.add(this.jbFail);//添加"認輸"按鈕
this.jbYChallenge.setBounds(5,190,86,20);
jpy.add(this.jbYChallenge);//添加"接受挑戰"按鈕
this.jbNChallenge.setBounds(100,190,86,20);
jpy.add(this.jbNChallenge);//添加"拒絕挑戰"按鈕
jpz.setLayout(null);//將棋盤設為空布局
jpz.setBounds(0,0,700,700);//設置大小
}
public void addListener()//為相應控件注冊事件監聽器
{
this.jbConnect.addActionListener(this);//為"連接"按鈕注冊事件監聽器
this.jbDisconnect.addActionListener(this);//為"斷開"按鈕注冊事件監聽器
this.jbChallenge.addActionListener(this);//為"挑戰"按鈕注冊事件監聽器
this.jbFail.addActionListener(this);//為"認輸"按鈕注冊事件監聽器
this.jbYChallenge.addActionListener(this);//為"同意挑戰"按鈕注冊事件監聽器
this.jbNChallenge.addActionListener(this);//為"拒絕挑戰"按鈕注冊事件監聽器
}
public void initialState()//初始化狀態
{
this.jbDisconnect.setEnabled(false);//將"斷開"按鈕設為不可用
this.jbChallenge.setEnabled(false);//將"挑戰"按鈕設為不可用
this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用
this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用
this.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用
}
public void initialQiZi()//初始化棋子
{//初始化各個棋子
qiZi[0][0]=new QiZi(color1,"車",0,0);//0行0列
qiZi[1][0]=new QiZi(color1,"馬",1,0);//0行1列
qiZi[2][0]=new QiZi(color1,"相",2,0);
qiZi[3][0]=new QiZi(color1,"仕",3,0);
qiZi[4][0]=new QiZi(color1,"帥",4,0);
qiZi[5][0]=new QiZi(color1,"仕",5,0);
qiZi[6][0]=new QiZi(color1,"相",6,0);
qiZi[7][0]=new QiZi(color1,"馬",7,0);
qiZi[8][0]=new QiZi(color1,"車",8,0);
qiZi[1][2]=new QiZi(color1,"砲",1,2);
qiZi[7][2]=new QiZi(color1,"砲",7,2);
qiZi[0][3]=new QiZi(color1,"兵",0,3);
qiZi[2][3]=new QiZi(color1,"兵",2,3);
qiZi[4][3]=new QiZi(color1,"兵",4,3);
qiZi[6][3]=new QiZi(color1,"兵",6,3);
qiZi[8][3]=new QiZi(color1,"兵",8,3);
qiZi[0][9]=new QiZi(color2,"車",0,9);
qiZi[1][9]=new QiZi(color2,"馬",1,9);
qiZi[2][9]=new QiZi(color2,"象",2,9);
qiZi[3][9]=new QiZi(color2,"士",3,9);
qiZi[4][9]=new QiZi(color2,"將",4,9);
qiZi[5][9]=new QiZi(color2,"士",5,9);
qiZi[6][9]=new QiZi(color2,"象",6,9);
qiZi[7][9]=new QiZi(color2,"馬",7,9);
qiZi[8][9]=new QiZi(color2,"車",8,9);
qiZi[1][7]=new QiZi(color2,"炮",1,7);
qiZi[7][7]=new QiZi(color2,"炮",7,7);
qiZi[0][6]=new QiZi(color2,"卒",0,6);
qiZi[2][6]=new QiZi(color2,"卒",2,6);
qiZi[4][6]=new QiZi(color2,"卒",4,6);
qiZi[6][6]=new QiZi(color2,"卒",6,6);
qiZi[8][6]=new QiZi(color2,"卒",8,6);
}
public void initialFrame()//初始化窗體
{
this.setTitle("中國象棋--客戶端");//設置窗體標題
Image image=new ImageIcon("ico.gif").getImage();
this.setIconImage(image); //設置圖標
this.add(this.jsp);//添加JSplitPane
jsp.setDividerLocation(730);//設置分割線位置及寬度
jsp.setDividerSize(4);
this.setBounds(30,30,930,730);//設置窗體大小
this.setVisible(true);//設置可見性
this.addWindowListener(//為窗體添加監聽器
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);//退出
}
}
);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==this.jbConnect)
{//單擊"連接"按鈕
this.jbConnect_event();
}
else if(e.getSource()==this.jbDisconnect)
{//當單擊"斷開"按鈕時
this.jbDisconnect_event();
}
else if(e.getSource()==this.jbChallenge)
{//當單擊"挑戰"按鈕時
this.jbChallenge_event();
}
else if(e.getSource()==this.jbYChallenge)
{//當單擊"同意挑戰"按鈕時
this.jbYChallenge_event();
}
else if(e.getSource()==this.jbNChallenge)
{//當單擊"拒絕挑戰"按鈕時
this.jbNChallenge_event();
}
else if(e.getSource()==this.jbFail)
{//當單擊"認輸"按鈕時
this.jbFail_event();
}
}
public void jbConnect_event()
{//對單擊"連接"按鈕事件的業務處理代碼
int port=0;
try
{//獲得用戶輸入的斷口號并轉化為整型
port=Integer.parseInt(this.jtfPort.getText().trim());
}
catch(Exception ee)
{//不是整數,給出錯誤提示
JOptionPane.showMessageDialog(this,"端口號只能是整數","錯誤",
JOptionPane.ERROR_MESSAGE);
return;
}
if(port>65535||port<0)
{//端口號不合法,給出錯誤提示
JOptionPane.showMessageDialog(this,"端口號只能是0-65535的整數","錯誤",
JOptionPane.ERROR_MESSAGE);
return;
}
String name=this.jtfNickName.getText().trim();//獲得昵稱
if(name.length()==0)
{//昵稱為空,給出錯誤提示信息
JOptionPane.showMessageDialog(this,"玩家姓名不能為空","錯誤",
JOptionPane.ERROR_MESSAGE);
return;
}
try
{
sc=new Socket(this.jtfHost.getText().trim(),port);//創建Socket對象
cat=new ClientAgentThread(this);//創建客戶端代理線程
cat.start();//啟動客戶端代理線程
this.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用
this.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用
this.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用
this.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用
this.jbDisconnect.setEnabled(true);//將"斷開"按鈕設為可用
this.jbChallenge.setEnabled(true);//將"挑戰"按鈕設為可用
this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用
this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用
this.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用
JOptionPane.showMessageDialog(this,"已連接到服務器","提示",
JOptionPane.INFORMATION_MESSAGE);//連接成功,給出提示信息
}
catch(Exception ee)
{
JOptionPane.showMessageDialog(this,"連接服務器失敗","錯誤",
JOptionPane.ERROR_MESSAGE);//連接失敗,給出提示信息
return;
}
}
public void jbDisconnect_event()
{//對單擊"斷開"按鈕事件的業務處理代碼
try
{
this.cat.dout.writeUTF("<#CLIENT_LEAVE#>");//向服務器發送離開的信息
this.cat.flag=false;//終止客戶端代理線程
this.cat=null;
this.jtfHost.setEnabled(!false);//將用于輸入主機名的文本框設為可用
this.jtfPort.setEnabled(!false);//將用于輸入端口號的文本框設為可用
this.jtfNickName.setEnabled(!false);//將用于輸入昵稱的文本框設為可用
this.jbConnect.setEnabled(!false);//將"連接"按鈕設為可用
this.jbDisconnect.setEnabled(!true);//將"斷開"按鈕設為不可用
this.jbChallenge.setEnabled(!true);//將"挑戰"按鈕設為不可用
this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用
this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用
this.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
public void jbChallenge_event(){
//獲得用戶選中的挑戰對象
Object o=this.jcbNickList.getSelectedItem();
if(o==null||((String)o).equals("")) {//z前面是什么都沒有,后面是有""
JOptionPane.showMessageDialog(this,"請選擇對方名字","錯誤",
JOptionPane.ERROR_MESSAGE);//當未選中挑戰對象,給出錯誤提示信息
}
else{
String name2=(String)this.jcbNickList.getSelectedItem();//獲得挑戰對象
try{
this.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用
this.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用
this.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用
this.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用
this.jbDisconnect.setEnabled(!true);//將"斷開"按鈕設為不可用
this.jbChallenge.setEnabled(!true);//將"挑戰"按鈕設為不可用
this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用
this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用
this.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用
this.cat.tiaoZhanZhe=name2;//設置挑戰對象
this.caiPan=true;//將caiPan設為true//z棋盤可動了
this.color=0;//將color設為0//z自己變為紅棋
this.cat.dout.writeUTF("<#TIAO_ZHAN#>"+name2);//發送挑戰信息
JOptionPane.showMessageDialog(this,"已提出挑戰,請等待恢復...","提示",
JOptionPane.INFORMATION_MESSAGE);//給出信息發出的提示信息
}
catch(Exception ee){ee.printStackTrace();}
}
}
public void jbYChallenge_event(){
try{ //發送同意挑戰的信息
this.cat.dout.writeUTF("<#TONG_YI#>"+this.cat.tiaoZhanZhe);
this.caiPan=false;//將caiPan設為false//z棋盤不可動
this.color=1;//將color設為1//z為黑棋
this.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用
this.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用
this.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用
this.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用
this.jbDisconnect.setEnabled(!true);//將"斷開"按鈕設為不可用
this.jbChallenge.setEnabled(!true);//將"挑戰"按鈕設為不可用
this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用
this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用
this.jbFail.setEnabled(!false);//將"認輸"按鈕設為可用
}
catch(Exception ee){ee.printStackTrace();}
}
public void jbNChallenge_event(){
try{ //發送拒絕挑戰的信息
this.cat.dout.writeUTF("<#BUTONG_YI#>"+this.cat.tiaoZhanZhe);
this.cat.tiaoZhanZhe=null;//將tiaoZhanZhe設為空
this.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用
this.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用
this.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用
this.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用
this.jbDisconnect.setEnabled(true);//將"斷開"按鈕設為可用
this.jbChallenge.setEnabled(true);//將"挑戰"按鈕設為可用
this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用
this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用
this.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用
}
catch(Exception ee){ee.printStackTrace();}
}
public void jbFail_event(){
try{ //發送認輸的信息
this.cat.dout.writeUTF("<#RENSHU#>"+this.cat.tiaoZhanZhe);
this.cat.tiaoZhanZhe=null;//將tiaoZhanZhe設為空
this.color=0;//將color設為0
this.caiPan=false;//將caiPan設為false
this.next();//初始化下一局
this.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用
this.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用
this.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用
this.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用
this.jbDisconnect.setEnabled(true);//將"斷開"按鈕設為可用
this.jbChallenge.setEnabled(true);//將"挑戰"按鈕設為可用
this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用
this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用
this.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用
}
catch(Exception ee){ee.printStackTrace();}
}
public void next(){//初始化下一局,恢復棋盤
for(int i=0;i<9;i++){//將棋子數組都置空
for(int j=0;j<10;j++){
this.qiZi[i][j]=null;
}
}
this.caiPan=false;
this.initialQiZi();//重新初始化棋子
this.repaint();//重繪
}
public static void main(String args[])
{
new XiangQi();
}
}
~~~
結果
