• 大小: 15.57 KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-10-20
  • 语言: Java
  • 标签: 五子棋  

资源简介

用java实现的网络五子棋,允许多对玩家同时在线对弈

资源截图

代码片段和文件信息

package netwuziqi;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.*;

import javax.swing.*;

public class ChatRoom {
private Socket s;
private boolean iscontinue=true;
private Jframe frame;
private JTextArea area;
private JLabel label;
private JTextField field;
private JButton button;
private String username;
private BufferedReader br;
private PrintWriter pw;
public ChatRoom(){
frame=new Jframe();
area=new JTextArea(2040);
label=new JLabel();
field=new JTextField(20);
button=new JButton(“发送“);
}
private void createConnection(){
//do{
String hostname=“192.168.0.26“;
try {
s=new Socket(hostname6201);
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(s.getOutputStream());
} catch (IOException e) {
JOptionPane.showMessageDialog(frame “连接参数不正确清重新输入“);
}
//}while(s==null);

username=“玩家“+(int)(Math.random()*100);
label.setText(username+“:“);
}
private void init(){
 createConnection();
JScrollPane jsp=new JScrollPane(area);
JPanel panel=new JPanel();
panel.add(label);
panel.add(field);
panel.add(button);
area.setFont(new Font(“宋体“Font.BOLD20));
field.setFont(new Font(“宋体“Font.BOLD20));
frame.add(jspBorderLayout.CENTER);
frame.add(panelBorderLayout.SOUTH);
new Thread(){

public void run(){
System.out.println(“客户端线程开始“);
while(iscontinue){
try {
String str=br.readLine();
System.out.println(“客户端读取成功“);
area.append(str+“\n“);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}.start();
}
private void addEventHandler(){
button.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
pw.println(label.getText()+field.getText());
pw.flush();
field.setText(““);
}

});
frame.addWindowListener(new WindowAdapter(){

@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
int a=JOptionPane.showConfirmDialog(null JOptionPane.YES_NO_CANCEL_OPTION);
if(a==JOptionPane.YES_OPTION){
pw.println(label.getText()+“:%GOODBYE%“);
pw.flush();
try {
Thread.sleep(300);
iscontinue=false;
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.exit(0);
}else{
return;
}
}

});
}
public void showMe(){
init();
addEventHandler();
frame.setSize(450 450);
frame.setLocation(860260);
frame.setVisible(true);
frame.setDefaultCloseOperation(Jframe.DO_NOTHING_ON_CLOSE);
}
// public static void main(String[] args) {
// // TODO Auto-generated method stub
// new ChatRoom().showMe();
// }

}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件         92  2009-04-18 08:59  netwuziqi\55

     文件       1488  2009-07-17 20:56  netwuziqi\ChatRoom$1.class

     文件       1361  2009-07-17 20:56  netwuziqi\ChatRoom$2.class

     文件       1818  2009-07-17 20:56  netwuziqi\ChatRoom$3.class

     文件       4369  2009-07-17 20:56  netwuziqi\ChatRoom.class

     文件       2898  2009-05-08 23:59  netwuziqi\ChatRoom.java

     文件       3133  2009-07-17 20:56  netwuziqi\ControlCouple.class

     文件       2973  2009-04-26 19:25  netwuziqi\ControlCouple.java

     文件       6449  2009-07-17 20:56  netwuziqi\Wuziqi.class

     文件       4671  2009-06-11 23:05  netwuziqi\Wuziqi.java

     文件       1671  2009-07-17 20:56  netwuziqi\WuziqiServer.class

     文件        934  2009-04-29 03:17  netwuziqi\WuziqiServer.java

     目录          0  2009-12-28 15:26  netwuziqi

----------- ---------  ---------- -----  ----

                31857                    13


评论

共有 条评论