本文实例为大家分享了java实现简单聊天室的具体代码,供大家参考,具体内容如下
先整理下思路:
1.创建一个通信服务端,传入端口号和相关的流后等待客户端连接,并初始化图形界面。
2.创建一个JFrame,用于写出聊天的界面,这里界面名称由其他类传入。
3.把客户端创建的方法写进JFrame(当然这里很粗糙的方法)
4.设置按钮的监听事件,发送消息和离线功能
首先创建一个服务端的类
import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import com.jero.jframe.ChatSocketJFrame; //创建服务端 public class ChatServerSocket{ //构造初始化方法 public ChatServerSocket(String name,int port) throws IOException{ //创建Socket服务连接对象 ServerSocket serverSocket = new ServerSocket(port); //等待客户端连接 Socket client = serverSocket.accept(); //初始化聊天界面 try { new ChatSocketJFrame(name,client); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //关闭流 serverSocket.close(); } public static void main(String[] args) throws Exception{ new ChatServerSocket("服务端",8001);//启动服务端,指定网名和端口 } }
package com.jero.jframe; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.border.EmptyBorder; import com.jero.socket.ChatServerSocket; import javax.swing.JTextPane; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JTextArea; public class ChatSocketJFrame extends JFrame implements ActionListener { private Socket socket; private String name; private JButton BT_SEND,BT_LEAVE,BT_LOGIN; private JTextArea textArea; private JPanel contentPane; private JTextField textField; private PrintWriter cout; /** * Launch the application. * @throws Exception * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, IOException, Exception { //实例化界面以及客户端 new ChatSocketJFrame("客户端","127.0.0.1",8001); } /** * Create the frame. */ public ChatSocketJFrame(String name, Socket socket)throws Exception, IOException { //聊天室界面 super("聊天室"+name+" "+InetAddress.getLocalHost()+" "+socket.getPort()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 733, 519); this.textArea = new JTextArea(); this.textArea.setEditable(false); this.getContentPane().add(new JScrollPane(this.textArea)); JPanel panel = new JPanel(); this.getContentPane().add(panel, "South"); this.textField = new JTextField(16); panel.add(this.textField); BT_SEND = new JButton("发送"); panel.add(BT_SEND); BT_SEND.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.out.println("按钮已点击"); cout.println(name+"说: "+textField.getText()+"\n"); textArea.append("我说: "+textField.getText()+"\n"); textField.setText(""); } }); BT_LEAVE = new JButton("离线"); panel.add(BT_LEAVE); BT_LEAVE.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub textArea.append("我离线"); cout.println(name+"离线\n"+"bye"); BT_LEAVE.setEnabled(false); BT_SEND.setEnabled(false); } }); this.setVisible(true); //开始创建通信 this.socket = socket; this.name = name; //获得Socket的输出流并立即刷新 this.cout = new PrintWriter(socket.getOutputStream(),true); //把网名传给对方 this.cout.println(name); //获得Socket对象的输入流,创建字符输入流 BufferedReader buff = new BufferedReader(new InputStreamReader(socket.getInputStream())); //接受对方的网名 String aline = buff.readLine(); //显示对方发送的内容 textArea.append("连接"+aline+"成功!!\n"); aline = buff.readLine(); //如果内容不为空并且对方不离线,则相互传送消息 while(aline!=null&&!aline.equals("bye")){ textArea.append(aline+"\r\n"); aline = buff.readLine(); } //当对方离线时,关闭所有的流 buff.close(); cout.close(); socket.close(); //把按钮设置为不可操作 BT_SEND.setEnabled(false); BT_LEAVE.setEnabled(false); } /** * @wbp.parser.constructor */ public ChatSocketJFrame(String name,String host,int port) throws UnknownHostException, IOException, Exception{ //创建客户端 this(name,new Socket(host,port)); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //可以把动作监听写进此函数,这个就不演示了 } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。