📄 clientframe.java
字号:
label.setSize(180,20);
label.setLocation(30,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);
//zhu ce shi bai,tiao kuang kuang ,clear
login_frame.clear();
}
public void do_regok()//注册成功 显示客户端
{
login_frame.setVisible(false);
login_frame.dispose();
client_frame.setVisible(true);
}
public void do_logon(String str)//有用户登录 对list加以更新
{
String st = str;
lst.add(st);
user_online.add((new StringTokenizer (st)).nextToken());
}
//---------------------------------------------------------------------------
public void updateList()//更新用户状态信息的显示
{
lst.clear();
for(int i=0;i<user_online.size();i++)
lst.add((String)user_online.elementAt(i));
}
public void changeState(String name,String state)//将在线用户中用户name的状态设为state
{
for (int i=0;i<user_online.size ();i++)
{
StringTokenizer st1=new StringTokenizer((String)user_online.elementAt(i)) ;
if(st1.nextToken().equals(name))
{
user_online.remove(i);
user_online.add(name+" "+state);
}
}
lst.clear();
for(int i=0;i<user_online.size();i++)
{
lst.add((String)user_online.elementAt(i));
}
}
public void do_request(StringTokenizer st)//有人请求与你下棋 加以响应
{
adversary = st.nextToken();
System.out.println(own_name +" Accepted request from "+adversary);
dialog = new Dialog(this);
dialog.setLayout(null);
Label label=new Label(new String("玩家"+adversary+"请求与你对弈,如同意,你会是白方,对方将先下"));
dialog.add(label);
dialog.setLocation(600,400);
label.setSize(300,20);
label.setLocation(10,40);
Button confirm = new Button("确定");
confirm.setLocation(60,70);
confirm.setSize(80,20);
confirm.addActionListener(new ActionListener(){//确定按钮
public void actionPerformed(ActionEvent evt)
{
out.println(MsgType.ACCEPT+" "+own_name+" "+adversary);
out.flush();
clearBoard();
qipan.clear();
is_own_turn = false;
is_black = false;
dialog.dispose();
changeState(own_name,"busy");
changeState(adversary,"busy");
withhim.setEnabled(false);
lose.setEnabled(true);
}
});
Button cancel = new Button("拒绝");
cancel.setLocation(160,70);
cancel.setSize(80,20);
cancel.addActionListener(new ActionListener(){//取消按钮
public void actionPerformed(ActionEvent evt)
{
out.println(MsgType.REFUSE+" "+adversary);
out.flush();
dialog.dispose();
}
});
dialog.setSize(320,100);
dialog.add(confirm);
dialog.add(cancel);
dialog.show(true);
}
public void do_refuse()//对方拒绝了你的对弈请求
{
adversary = null;
dialog = new Dialog(this);
dialog.setLayout(null);
Label label=new Label("对方拒绝你的请求或对方正忙");
label.setSize(180,20);
label.setLocation(30,40);
dialog.add(label);
dialog.setLocation(600,400);
Button b = new Button("确定");
b.setSize(80,20);
b.setLocation(50,70);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
dialog.dispose();
withhim.setEnabled(true);
}
});
dialog.add(b);
dialog.setSize(200,100);
dialog.show(true);
adversary=null;
}
public void do_accept(StringTokenizer st)//对方接受了你的请求
{
String from = st.nextToken();
String to = st.nextToken();
changeState(from,"busy");
changeState(to,"busy");
qipan.clear();
is_own_turn = true;
is_black = true;
lose.setEnabled(true);
}
public void do_putchess(StringTokenizer st)//对手下了一步棋
{
int row = Integer.parseInt(st.nextToken());
int col = Integer.parseInt(st.nextToken());
qipan.addChess(row, col, is_black ? 2 : 1);
is_own_turn=true;
player1.setText(" Player:: "+own_name+" is thinking now...");
player2.setText(" Player:: "+adversary+" just put at ["+row+","+col+"]");
if(qipan.isOver(row, col))
//对手赢的该局
{
out.println(""+MsgType.WIN+" "+ adversary+" "+ own_name);
qipan.clear();
dialog = new Dialog(this);
dialog.setLayout(null);
Label label=new Label("U lose");
label.setSize(180,20);
label.setLocation(30,40);
dialog.add(label);
dialog.setLocation(600,400);
Button b = new Button("确定");
b.setSize(80,20);
b.setLocation(50,70);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
dialog.dispose();
qipan.clear();
adversary=null;
}
});
dialog.add(b);
dialog.setSize(200,100);
dialog.show(true);
withhim.setEnabled(true);
lose.setEnabled(false);
}
}
public void setOwnTurn(boolean con)
{
is_own_turn = con;
}
public boolean isOwnTurn()
{
return is_own_turn;
}
public void setName(String name)
{
own_name = name;
}
public void clearBoard()//清棋盘
{
for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
chess_board[i][j] = 0;
}
private void updateChessTable()
{
// 更新棋桌//shua xin shu zu li de zhi,quan bu wei 0
}
public void do_gameend(StringTokenizer st)//实际上没有调用
{
String user1 = st.nextToken();
String user2 = st.nextToken();
changeState(user1,"free");
changeState(user2,"free");
}
public void do_gamestart(StringTokenizer st)//实际上没有调用
{
String user1 = st.nextToken();
String user2 = st.nextToken();
changeState(user1,"busy");
changeState(user2,"busy");
}
public void do_loss()//对方已经认输
{
dialog = new Dialog(this);
dialog.setLayout(null);
Label label=new Label("你赢了");
label.setSize(180,20);
label.setLocation(30,40);
dialog.add(label);
dialog.setLocation(600,400);
Button b = new Button("确定");
b.setSize(80,20);
b.setLocation(50,70);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
dialog.dispose();
qipan.clear();
withhim.setEnabled(true);
lose.setEnabled(false);
}
});
dialog.add(b);
dialog.setSize(200,100);
dialog.show(true);
adversary = null;
}
public void do_win()
{
dialog = new Dialog(this);
dialog.setLayout(null);
Label label=new Label("U win");
label.setSize(180,20);
label.setLocation(30,40);
dialog.add(label);
dialog.setLocation(600,400);
Button b = new Button("确定");
b.setSize(80,20);
b.setLocation(50,70);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
dialog.dispose();
withhim.setEnabled(true);
lose.setEnabled(false);
}
});
dialog.add(b);
dialog.setSize(200,100);
dialog.show(true);
adversary = null;
is_own_turn=false;
qipan.clear();
}
public void do_draw() //处理对方和棋请求
{
dialog = new Dialog(this);
dialog.setLayout(null);
Label label=new Label("对方请求和棋,是否同意?");
label.setSize(180,20);
label.setLocation(30,40);
dialog.add(label);
dialog.setLocation(600,400);
Button b = new Button("确定");
b.setSize(80,20);
b.setLocation(50,70);
Button c = new Button("拒绝");
c.setSize(80,20);
c.setLocation(150,70);
b.addActionListener(new ActionListener(){//拒绝
public void actionPerformed(ActionEvent evt)
{
changeState(own_name,"free");
changeState(adversary,"free");
//adversary = null;
out.println(MsgType.DRAWOK+" "+own_name+" "+adversary);
out.flush();
adversary = null;
dialog.dispose();
qipan.clear();
withhim.setEnabled(true);
}
});
dialog.add(b);
c.addActionListener(new ActionListener(){//同意
public void actionPerformed(ActionEvent evt)
{
dialog.dispose();
out.println(MsgType.DRAWFAIL+" "+adversary);
out.flush();
}
});
dialog.add(c);
dialog.setSize(200,100);
dialog.show(true);
}
public void do_drawfail() //不同意和棋 通过服务器中转该信息
{
dialog = new Dialog(this);
dialog.setLayout(null);
Label label=new Label("对方拒绝和棋");
label.setSize(180,20);
label.setLocation(30,40);
dialog.add(label);
dialog.setLocation(600,400);
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.setSize(200,100);
dialog.show(true);
}
public void do_drawok() //和棋要求被接受
{
changeState(own_name,"free");
changeState(adversary,"free");
adversary = null;
dialog = new Dialog(this);
dialog.setLayout(null);
Label label=new Label("对方同意和棋");
label.setSize(180,20);
label.setLocation(30,40);
dialog.add(label);
dialog.setLocation(600,400);
Button b = new Button("确定");
b.setSize(80,20);
b.setLocation(50,70);
is_own_turn=false;
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
dialog.dispose();
qipan.clear();
withhim.setEnabled(true);
}
});
dialog.add(b);
dialog.setSize(200,100);
dialog.show(true);
}
//------------------------------------------------------------------------
public static void main(String args[])
{
try
{
String ip = null;
if(args.length>0)
ip = args[0];
InetAddress addr = InetAddress.getByName(ip);
Socket socket = new Socket(addr,MsgType.PORT);
System.out.println(socket);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
}
catch(Exception e){
e.printStackTrace();
}
client_frame = new ClientFrame();
client_frame.setLocation(100,50);
(new ServerConnection(client_frame,in)).start();
login_frame = new LoginFrame(client_frame);
login_frame.setLocation(350,250);
}
}
class ScoreFrame extends Frame
{
public ScoreFrame()
{
this.setTitle("查看积分");
this.setSize(220,700);
this.setLayout(null);
this.setVisible(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -