📄 clientframe.java
字号:
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class ClientFrame extends Frame
{
Panel pleft;
TextArea tachat;
TextField playername,tfchat;
java.awt.List lst;
Button send,choseplayer,showonline,gameconfig,gameCntexit;
Button leftbar,withhim,rightbar,upbar,upbar1,centerbar,downbar,getdraw,lose,redo;
TextField choseuser;
ChessRoom qipan;
Label player1,player2;
static PrintWriter out;
static BufferedReader in;
static LoginFrame login_frame;
static ClientFrame client_frame;
Vector user_online = new Vector();
int[][] chess_board = new int[15][15]; // 0 表示无子,1 表示自己的 2表示对方的
boolean is_own_turn = false;
Dialog dialog;
String adversary = null;
String own_name;
boolean is_black = false;
boolean is_busy = false;
ScoreFrame aa=new ScoreFrame();
public ClientFrame()
{
// setTitle();
this.setBackground (Color.DARK_GRAY);
setSize(750,676);
this.setResizable (false);
setLayout(null);
pleft =new Panel();
pleft.setSize (450,450);
pleft.setBackground (Color.DARK_GRAY);
add(pleft);
pleft.setLocation (0,0);
qipan=new ChessRoom(this);
pleft.add(qipan);
tfchat=new TextField();
tfchat.setSize (325,26);
tfchat.setBackground (Color.DARK_GRAY);
tfchat.setForeground (Color.white);
add(tfchat);
tfchat.setLocation (3,641);
tfchat.setFont (new Font("verdana",Font.PLAIN,15) );
tfchat.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String msg = tfchat.getText();
boolean youren = false;
String duifang_p = lst.getSelectedItem();
int index = lst.getSelectedIndex();
if(duifang_p!=null)
{
StringTokenizer st = new StringTokenizer(duifang_p);
String duifang = st.nextToken();
out.println(MsgType.CHAT+" "+own_name+" "+duifang+" "+msg);
lst.deselect(index);
tachat.append(">>>"+own_name+" 对"+" "+duifang+"说:"+msg+"\n");
}
else{
out.println(MsgType.CHAT+" "+own_name+" all "+msg);
tachat.append(">>>"+own_name+" 对"+"所有人说:"+msg+"\n");
}
out.flush();
tfchat.setText("");
}
});
tachat=new TextArea();
tachat.setSize (450,185);
tachat.setBackground (Color.DARK_GRAY);
tachat.setForeground (Color.white);
tachat.setEditable(false);
add(tachat);
tachat.setLocation (2,455);
send=new Button("发送话语");
send.setForeground (Color.blue);
send.setSize (120,26);
send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
String msg = tfchat.getText();
boolean youren = false;
String duifang_p = lst.getSelectedItem();
int index = lst.getSelectedIndex();
if(duifang_p!=null)
{
StringTokenizer st = new StringTokenizer(duifang_p);
String duifang = st.nextToken();
out.println(MsgType.CHAT+" "+own_name+" "+duifang+" "+msg);
lst.deselect(index);
tachat.append(">>>"+own_name+" 对"+" "+duifang+"说:"+msg+"\n");
}
else{
out.println(MsgType.CHAT+" "+own_name+" all "+msg);
tachat.append(">>>"+own_name+" 对"+"所有人说:"+msg+"\n");
}
out.flush();
tfchat.setText("");
}
});
add(send);
send.setLocation (329,641);
showonline=new Button("people online(status)");
showonline.setForeground (Color.blue);
showonline.setSize (280,25);
showonline.setFocusable (false);
add(showonline);
showonline.setLocation (460,24);
lst=new java.awt.List();
lst.setSize (280,349);
lst.setLocation (460,49);
lst.setForeground (Color.white);
lst.setBackground (Color.DARK_GRAY);
// lst.add("all free");
////////////////////////////////////////////
add(lst);
//lst.add(">>> eric (free)");
//lst.add(">>> eric531 (busy)");
//lst.add(">>> eric (free)");
gameconfig=new Button("游戏操作");
gameconfig.setForeground (Color.blue);
gameconfig.setSize (280,25);
gameconfig.setLocation (460,500);
//gameconfig.setEnabled (false);
gameconfig.setFocusable (false);
add(gameconfig);
gameCntexit=new Button("退出游戏客户端");
gameCntexit.setForeground (Color.blue);
gameCntexit.setSize (280,35);
gameCntexit.setLocation (460,633);
add(gameCntexit);
getdraw=new Button("请求和棋");
getdraw.setForeground (Color.blue);
getdraw.setSize (93,108);
getdraw.setLocation (460,520);
getdraw.addActionListener (new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(adversary!=null)
out.println(MsgType.DRAW+" "+adversary);
out.flush();
}
});
add(getdraw);
redo=new Button("查看积分");
redo.setForeground (Color.blue);
redo.setSize (93,108);
redo.setLocation (553,520);
aa.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt)
{
aa.setVisible(false);
}
});
redo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
aa.setVisible(true);
java.awt.List scorelist=lst;
aa.add(scorelist);
scorelist.setSize(200,600);
scorelist.setLocation(10,40);
}
});
add(redo);
// redo.setEnabled(false);
lose=new Button("认输");
lose.setForeground (Color.blue);
lose.setSize (93,108);
lose.setLocation (646,520);
lose.setEnabled(false);
lose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
if(adversary!=null)
out.println(MsgType.LOSS+" "+own_name+" "+adversary);
changeState(own_name,"free");
changeState(adversary,"free");
adversary = null;
is_own_turn=false;
qipan.clear();
withhim.setEnabled(true);
lose.setEnabled(false);
}
});
add(lose);
withhim=new Button("我要和他下棋");
withhim.setForeground (Color.blue);
withhim.setSize (90,25);
withhim.setLocation (650,399);
withhim.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
adversary = choseuser.getText();
if(!adversary.equals(own_name) && adversary.length()!=0 )
out.println(MsgType.REQUEST+" "+own_name+" "+adversary);
withhim.setEnabled(false);
}
});
add(withhim);
choseuser=new TextField();
choseuser.setSize(189,27);
choseuser.setLocation(460,398);
choseuser.setText("");
choseuser.setForeground(Color.white);
choseuser.setBackground (Color.DARK_GRAY);
choseuser.setFont (new Font("verdana",Font.PLAIN,15) );
add(choseuser);
//add player status
player1=new Label("Player:: ");
player1.setSize(200,20);
player1.setLocation(465,435);
add(player1);
player1.setForeground(Color.white);
player2=new Label("Player:: ");
player2.setSize(200,20);
player2.setLocation(465,460);
add(player2);
player2.setForeground(Color.white);
leftbar=new Button();
leftbar.setSize (10,668);
leftbar.setLocation(450,0);
leftbar.setEnabled (false);
add(leftbar);
rightbar=new Button();
rightbar.setSize (10,676);
rightbar.setLocation(740,0);
rightbar.setEnabled (false);
add(rightbar);
downbar=new Button();
downbar.setSize (750,5);
downbar.setLocation (0,668);
downbar.setEnabled (false);
downbar.setFocusable (false);
add(downbar);
centerbar=new Button();
centerbar.setSize (280,5);
centerbar.setLocation (460,628);
centerbar.setEnabled (false);
centerbar.setFocusable (false);
add(centerbar);
upbar=new Button();
upbar.setSize (280,5);
upbar.setLocation (460,495);
upbar.setEnabled (false);
upbar.setFocusable (false);
add(upbar);
upbar1=new Button();
upbar1.setSize (280,5);
upbar1.setLocation (460,423);
upbar1.setEnabled (false);
upbar1.setFocusable (false);
add(upbar1);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
String name_full = own_name+" "+(is_busy?"busy":"free");
if(adversary!=null)
name_full += " "+adversary;
out.println(MsgType.LOGOUT+" "+name_full);
out.flush();
System.exit(0);
}
} );
gameCntexit.addActionListener (new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String name_full = own_name+" "+(is_busy?"busy":"free");
if(adversary!=null)
name_full += " "+adversary;
out.println(MsgType.LOGOUT+" "+name_full);
out.flush();
System.exit(0);
}
}
);
}
//chat and else -----------------------------------------------------------------------
public void updateChatArea(String from,String to,String msg)//更新聊天area
{
client_frame.tachat.append(">>>"+from+" 对 "+to+"说:"
+msg+"\n");
}
public void do_chat(StringTokenizer st,String message)//收到聊天信息 加以处理
{
String from = st.nextToken();
String to = st.nextToken();
String msg = message.substring(from.length()+to.length()+2,message.length());
if(to.equals("all"))
tachat.append(">>>"+own_name+" 对"+"所有人说:"+msg+"\n");
else
updateChatArea(from,to,msg);
}
public void do_sendlist(StringTokenizer st)//上线后要求服务器发送在线用户信息,加入到list中
{
lst.clear();
while(st.hasMoreTokens())
{
String ss1=st.nextToken();
String ss2=st.nextToken();
String ss3=st.nextToken();
String S=ss1+" status:("+ss2+") score: "+ss3;
lst.add(S);
}
}
//---------------------------------------------------------------------------
//login window-----------------------------------------------------------------------------------
public void do_online() //用户已经在线
{
dialog = new Dialog(this);
dialog.setLayout(null);
dialog.setSize(200,100);
dialog.add(new Label("请不要重复登陆"));
Button aaaa = new Button("确定");
class myListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
dialog.dispose();
}
}
aaaa.addActionListener(new myListener());
aaaa.setLocation(150,60);
dialog.add(aaaa);
login_frame.clear();
}
public void do_passwdok()//登录成功 打开用户界面
{
login_frame.dispose();
client_frame.setVisible(true);
}
public void do_wrongpasswd()//用户名无匹配信息或者密码输入错误
{
//pop up a dialog!!!
dialog=new Dialog(login_frame,"Attention!!!",true);
dialog.setLayout(null);
dialog.setSize(200,100);
dialog.setLocation(600,400);
Label label=new Label("用户名或密码错误或重复登陆!!");
label.setSize(180,20);
label.setLocation(22,40);
dialog.add(label);
Button b = new Button("确定");
b.setSize(80,20);
b.setLocation(50,70);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
dialog.dispose();
}
});
dialog.add(b);
dialog.show(true);
//shu cuo mi ma shi hou .....
login_frame.clear();
}
public void do_regfail()
{
//pop up a dialog;
dialog=new Dialog(login_frame,"Attention!!!",true);
dialog.setLayout(null);
dialog.setSize(200,100);
Label label=new Label(" 注册失败");
dialog.setLocation(600,400);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -