1、import java.awt.*;import java.awt.event.*;import java.io.*;import .*;import java.util.*;class clientThread extends ThreadchessClient chessclient;clientThread(chessClient chessclient)this.chessclient=chessclient;public void acceptMessage(String recMessage)if(recMessage.startsWith(/userlist )StringTok
2、enizer 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(userNumber0 & !user.startsWith
3、(inchesschessclient.userpad.userList.add(user);chessclient.inputpad.userChoice.addItem(user);userNumber+;chessclient.inputpad.userChoice.select(else if(recMessage.startsWith(/yourname chessclient.chessClientName=recMessage.substring(10);chessclient.setTitle(Java五子棋客户端 +用户名:+chessclient.chessClientNa
4、me);else if(recMessage.equals(/rejecttrychessclient.chesspad.statusText.setText(不能加入游戏chessclient.controlpad.cancelGameButton.setEnabled(false);chessclient.controlpad.joinGameButton.setEnabled(true);chessclient.controlpad.creatGameButton.setEnabled(true);catch(Exception ef)chessclient.chatpad.chatLi
5、neArea.setText(chessclient.chesspad.chessSocket.close无法关闭/peer chessclient.chesspad.chessPeerName=recMessage.substring(6);if(chessclient.isServer)chessclient.chesspad.chessColor=1;chessclient.chesspad.isMouseEnabled=true;请黑棋下子else if(chessclient.isClient)chessclient.chesspad.chessColor=-1;已加入游戏,等待对方
6、下子./youwinchessclient.isOnChess=false;chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor);对方退出,请点放弃游戏退出连接chessclient.chesspad.isMouseEnabled=false;/OK创建游戏成功,等待别人加入./errorchessclient.chatpad.chatLineArea.append(传输错误:请退出程序,重新加入 nelsechessclient.chatpad.chatLineArea.append(recMessage+nch
7、essclient.chatpad.chatLineArea.setCaretPosition(chessclient.chatpad.chatLineArea.getText().length();public void run()String message=;while(true)message=chessclient.in.readUTF();acceptMessage(message);catch(IOException es)public class chessClient extends Frame implements ActionListener,KeyListeneruse
8、rPad userpad=new userPad();chatPad chatpad=new chatPad();controlPad controlpad=new controlPad();chessPad chesspad=new chessPad();inputPad inputpad=new inputPad();Socket chatSocket;DataInputStream in;DataOutputStream out;String chessClientName=null;String host=null;int port=4331;boolean isOnChat=fals
9、e; /在聊天?boolean isOnChess=false; /在下棋?boolean isGameConnected=false; /下棋的客户端连接?boolean isServer=false; /如果是下棋的主机boolean isClient=false; /如果是下棋的客户端Panel southPanel=new Panel();Panel northPanel=new Panel();Panel centerPanel=new Panel();Panel westPanel=new Panel();Panel eastPanel=new Panel();chessClien
10、t()super(Java五子棋客户端setLayout(new BorderLayout();host=controlpad.inputIP.getText();westPanel.setLayout(new BorderLayout();westPanel.add(userpad,BorderLayout.NORTH);westPanel.add(chatpad,BorderLayout.CENTER);westPanel.setBackground(Color.pink);inputpad.inputWords.addKeyListener(this);chesspad.host=con
11、trolpad.inputIP.getText();centerPanel.add(chesspad,BorderLayout.CENTER);centerPanel.add(inputpad,BorderLayout.SOUTH);centerPanel.setBackground(Color.pink);controlpad.connectButton.addActionListener(this);controlpad.creatGameButton.addActionListener(this);controlpad.joinGameButton.addActionListener(t
12、his);controlpad.cancelGameButton.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(Color.pink);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)if(isOnChat)chat
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1