2018.4.28 基于java的聊天系统(带完善)

步骤:

   第一步: ChatUtil工具类:把一些常用的常量放进来

    第二步:Server开启服务
    第三步:ClientSocket连接服务器的socket
    第四步:CHatFrame(添加两个属性(name,sex))2.添加了getSocket方法
    第五步:LoginFrame设置了默认值  处理性别获得socket对象
package com.lanqiao.demo2;
/**
 * 工具类
 * @author qichunlin
 *
 */
public final class ChatUtil {
    //地址
    public static final String ADDRESS = "localhost";
    //端口
    public static final int PORT = 9999;
}
package com.lanqiao.demo2;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务端类
 * @author qichunlin
 */
public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket ss = new ServerSocket(ChatUtil.PORT);
            int count = 0;
            while (true) {
                System.out.println("等待客户端连接.......");
                Socket socket = ss.accept();
                count++;
                System.out.println("目前有"+count+"个客户端进入了聊天室");
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
package com.lanqiao.demo2;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 客户端的socket
 * @author qichunlin
 *
 */
public class ClientSocket {
    public static Socket socket;
    public ClientSocket() {
        try {
            socket = new Socket(ChatUtil.ADDRESS, ChatUtil.PORT);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
package com.lanqiao.demo2;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
 * 聊天系统的登录界面
 * @authorqichunlin
 *
 */
public class LoginFrame extends JFrame implements ActionListener{
    //定义组件
    JLabel userLab,addrLab,portLab;//标签
    JTextField userText,addrText,portText;//文本框
    JRadioButton radioMan,radioWoman,radioser;//单选按钮
    ButtonGroup group ;//组
    JButton connectBut,closeBut;//按钮
    //容器
    JPanel p1,p2,p3;
    
    public LoginFrame(){
        //实例化组件
        p1 = new JPanel();
        p1.setLayout(new FlowLayout(FlowLayout.LEFT));
        
        userLab = new JLabel("姓名:");
        userText = new JTextField(10);
        radioMan = new JRadioButton("男");
        radioMan.setSelected(true);
        radioWoman = new JRadioButton("女");
        radioser = new JRadioButton("保密"); 
        //把单选按钮,添加到组中
        group = new ButtonGroup();
        group.add(radioMan);
        group.add(radioWoman);
        group.add(radioser);
        
        //往p1中,添加组件了(注意:组不需要添加到容器中)
        p1.add(userLab);
        p1.add(userText);
        p1.add(radioMan);
        p1.add(radioWoman);
        p1.add(radioser);
        
        
        p2 = new JPanel();
        p2.setLayout(new FlowLayout(FlowLayout.LEFT));
        addrLab = new JLabel("地址:");
        
        addrText = new JTextField(10);
        addrText.setText(ChatUtil.ADDRESS);
        
        portLab = new JLabel("端口:");
        portText = new JTextField(10);
        portText.setText(ChatUtil.PORT+"");
        
        //把组件添加到p2中
        p2.add(addrLab);
        p2.add(addrText);
        p2.add(portLab);
        p2.add(portText);
        
        
        p3 = new JPanel();
        p3.setLayout(new FlowLayout(FlowLayout.CENTER));
        
        connectBut =new JButton("连接");
        //绑定事件【点击事件】
        connectBut.addActionListener(this);
        
        closeBut = new JButton("断开");
        //把组件添加到p3中
        p3.add(connectBut);
        p3.add(closeBut);
        
        
        
        
        
        //设置面板的布局模式(流式布局)
        this.getContentPane().setLayout(new GridLayout(3,1));//网格布局
        
        //把组件添加到面板了
        //1、获取面板
        Container c =  this.getContentPane();
        //把p1容器添加到面板
        c.add(p1);
        //把p2容器添加到面板
        c.add(p2);
        //把p3容器添加到面板
        c.add(p3);
        
        init();
    }
    /**
     * 初始化窗体的基本信息
     */
    public void init(){
        //1、标题
        this.setTitle("登录界面");
        //2、大小
        this.setSize(350,200);
        //3、关闭放大功能
        this.setResizable(false);
        //4、位置
        this.setLocationRelativeTo(null);
        //5、是否显示
        this.setVisible(true);
        //6、关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public static void main(String[] args) {
        new LoginFrame();
    }
    //点击事件的处理过程
    @Override
    public void actionPerformed(ActionEvent e) {
        //处理选择中的性别
        String sex = "";
        if(radioWoman.isSelected()) {
            sex = "女";
        }else if(radioMan.isSelected()) {
            sex = "男";
        }else {
            sex = "保密";
        }
        System.out.println("============");
        //1、隐藏当前的界面【登录界面】
        this.setVisible(false);
        //2、显示聊天的界面
        ChatFrame c = new ChatFrame(userText.getText(),sex);//登录名传递过来
        c.getSocket();
    }
}


package com.lanqiao.demo2;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务端类
 * @author qichunlin
 */
public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket ss = new ServerSocket(ChatUtil.PORT);
            int count = 0;
            while (true) {
                System.out.println("等待客户端连接.......");
                Socket socket = ss.accept();
                count++;
                System.out.println("目前有"+count+"个客户端进入了聊天室");
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/qichunlin/p/8969894.html