📄 omokclient.java
字号:
//computerButton.setEnabled(false);
startButton.setEnabled(false);
stopButton.setEnabled(false);
p2.setBounds(500,110,250,180);
Panel p3=new Panel();
p3.setLayout(new BorderLayout());
p3.add(msgView,"Center");
p3.add(sendBox,"South");
p3.setBounds(500,300,250,250);
add(p);
add(p2);
add(p3);
//注册事件监听器
sendBox.addActionListener(this);
enterButton.addActionListener(this);
exitButton.addActionListener(this);
startButton.addActionListener(this);
stopButton.addActionListener(this);
//computerButton.addActionListener(this);
setLookAndFeel(this);//设置Windows界面风格
//关闭窗口
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
//组件动作事件处理
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==sendBox){ //若为消息输入框
String msg=sendBox.getText();
if(msg.length()==0)return;
if(msg.length()>=30)msg=msg.substring(0,30);
try{
writer.println("[MSG]"+msg);
sendBox.setText("");
}
catch(Exception e){
}
}
else if(ae.getSource()==enterButton){ //若为入场按纽
try{
if(Integer.parseInt(roomBox.getText())<1){
infoView.setText("房间号错,大于1");
return;
}
writer.println("[ROOM]"+Integer.parseInt(roomBox.getText()));
msgView.setText("");
}
catch(Exception ie){
infoView.setText("输入的事项发生错误");
}
}
else if(ae.getSource()==exitButton){ //若为退出按纽
try{goToWaitRoom();
startButton.setEnabled(false);
stopButton.setEnabled(false);
}
catch(Exception e){
}
}
else if(ae.getSource()==startButton){ //若为对决开始按纽
try{
writer.println("[START]");
infoView.setText("请等待对方决定");
startButton.setEnabled(false);
}
catch(Exception e){
}
}
else if(ae.getSource()==stopButton){ //若为弃权按纽
try{
writer.println("[DROPGAME]");
endGame("已弃权");
}
catch(Exception e){
}
}
}
void setLookAndFeel(Component o)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(o);
}
catch(Exception e)
{
System.err.println("Couln't use the System "+"look and feel: "+e);
}
}
void goToWaitRoom(){ //点击进入待机室按纽时被调用
if(userName==null){
String name=nameBox.getText().trim();
if(name.length()<=2||name.length()>10){
infoView.setText("名字错误");
nameBox.requestFocus();
return;
}
userName=name;
writer.println("[NAME]"+userName);
nameBox.setText(userName);
nameBox.setEditable(false);
}
msgView.setText("");
writer.println("[ROOM]0");
infoView.setText("已进入待机室");
roomBox.setText("0");
enterButton.setEnabled(true);
exitButton.setEnabled(false);
}
public void run(){ //来自服务器消息
String msg;
try{
while((msg=reader.readLine())!=null){
if(msg.startsWith("[STONE]")){
String temp=msg.substring(7);
int x=Integer.parseInt(temp.substring(0,temp.indexOf(" ")));
int y=Integer.parseInt(temp.substring(temp.indexOf(" ")+1));
board.putOpponent(x,y); //绘制对方棋子
board.setEnable(true);
}
else if(msg.startsWith("[ROOM]")){ //进入房间
if(!msg.equals("[ROOM]")){ //若非待机室
enterButton.setEnabled(false);
exitButton.setEnabled(true);
infoView.setText(msg.substring(6)+"号房间已经被进入");
}
else infoView.setText("已经进入待机室");
roomNumber=Integer.parseInt(msg.substring(6)); //指定房间号
if(board.isRunning()){ //若处于游戏状态
board.stopGame(); //终止游戏
}
}
else if(msg.startsWith("[FULL]")){ //若房间已满
infoView.setText("房间满员,禁止入内");
}
else if(msg.startsWith("[PLAYERS]")){ //房中用户名单
nameList(msg.substring(9));
}
else if(msg.startsWith("[ENTER]")){ //
pList.add(msg.substring(7));
playersInfo();
msgView.append("["+msg.substring(7)+"]入场.\n");
}
else if(msg.startsWith("[EXIT]")){ //退出
pList.remove(msg.substring(6)); //从列表中删除
playersInfo(); //重新计算并显示人数
msgView.append("["+msg.substring(6)+"]进入其他房间.\n");
if(roomNumber!=0)
endGame("对方离开。");
}
else if(msg.startsWith("[DISCONNECT]")){ //连接中断
pList.remove(msg.substring(12));
playersInfo();
msgView.append("["+msg.substring(12)+"]中断连接.\n");
if(roomNumber!=0)
endGame("对方离开。");
}
else if(msg.startsWith("[COLOR]")){
String color=msg.substring(7);
board.startGame(color); //开始游戏
if(color.equals("BLACK"))
infoView.setText("得到黑子");
else
infoView.setText("得到白子");
stopButton.setEnabled(true);
} //激活弃权按纽
else if(msg.startsWith("[DROPGAME]")) //若对方弃权
endGame("对方弃权");
else if(msg.startsWith("[WIN]")) //若已经获胜
endGame("获胜");
else if(msg.startsWith("[LOSE]")) //若失败
endGame("失败");
else msgView.append(msg+"\n");
}
}
catch(IOException ie){
msgView.append(ie+"\n");
}msgView.append("连接中断");
}
private void endGame(String msg){ //终止游戏
infoView.setText(msg);
startButton.setEnabled(false);
stopButton.setEnabled(false);
try{ //等待两秒
Thread.sleep(2000);
}
catch(Exception e){
}
if(board.isRunning())board.stopGame();
if(pList.getItemCount()==2)startButton.setEnabled(true);
}
private void playersInfo(){ //显示房间连接的人数
int count=pList.getItemCount();
if(roomNumber==0)
pInfo.setText("待机室:"+count+"名");
if(count==2&&roomNumber!=0)
startButton.setEnabled(true);
else startButton.setEnabled(false);
}
private void nameList(String msg){
pList.removeAll();
StringTokenizer st=new StringTokenizer(msg,"\t");
while(st.hasMoreElements())
pList.add(st.nextToken());
playersInfo();
}
private void connect(){ //连接
try{
msgView.append("请求连接服务器.\n");
socket=new Socket("127.0.0.1",7777);
msgView.append("---连接成功---.\n");
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer=new PrintWriter(socket.getOutputStream(),true);
new Thread(this).start();
board.setWriter(writer);
}
catch(Exception e){
msgView.append(e+"\n\n连接失败..\n");
}
}
public static void main(String[] args){
OmokClient client=new OmokClient("网络五子棋游戏");
client.setSize(760,560);
client.setVisible(true);
client.connect();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -