您好,登錄后才能下訂單哦!
本文實例為大家分享了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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。