欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實現(xiàn)多人聊天室可視化

 更新時間:2022年06月10日 15:43:01   作者:丶di  
這篇文章主要為大家詳細介紹了java實現(xiàn)多人聊天室可視化,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

編寫一個 Java 應(yīng)用程序,實現(xiàn)圖形界面多人聊天室(多線程實現(xiàn)),要求聊天室窗口標(biāo)題是 “歡迎使用 XXX 聊天室應(yīng)用”,其中 XXX 是自己的班級姓名學(xué)號,如“軟件 171 張三 1234”。

客戶端

import java.awt.BorderLayout;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import java.io.*;
class Login extends JFrame implements ActionListener{
?? ?//用戶名
?? ?JPanel jp1=new JPanel();
?? ?JLabel jl1=new JLabel("用戶名");
?? ?public JTextField jtf1=new JTextField(15);
?? ?//密碼
?? ?JPanel jp2=new JPanel();
?? ?JLabel jl2=new JLabel("密碼");
?? ?JPasswordField jpf2 = new JPasswordField(15);
?? ?//登入取消按鈕
?? ?JPanel jp3=new JPanel();
?? ?JButton jbt1=new JButton("登入");
?? ?JButton jbt2=new JButton("取消");
?? ?
?? ?public Login() {
?? ??? ?// TODO 自動生成的構(gòu)造函數(shù)存根
?? ??? ?this.setTitle("客服端登入窗口");
?? ??? ?Container con =this.getContentPane();
?? ??? ?con.setLayout(new FlowLayout());
?? ??? ?//用戶名
?? ??? ?jp1.add(jl1);
?? ??? ?jp1.add(jtf1);
?? ??? ?jtf1.addActionListener(this);
?? ??? ?//密碼
?? ??? ?jpf2.setEchoChar('*'); ?//用*顯示密碼框輸入的數(shù)據(jù)
?? ??? ?jp2.add(jl2);
?? ??? ?jp2.add(jpf2);
?? ??? ?jpf2.addActionListener(this);
?? ??? ?//登入取消按鈕
?? ??? ?jp3.add(jbt1);
?? ??? ?jp3.add(jbt2);
?? ??? ?//添加到當(dāng)前窗口容器
?? ??? ?con.add(jp1);
?? ??? ?con.add(jp2);
?? ??? ?con.add(jp3);
?? ??? ?this.setSize(500, 300); ? ?//設(shè)置窗體大小
?? ??? ?setLocationRelativeTo(null); //設(shè)置窗口居中
?? ??? ?this.setResizable(false); ?//窗體大小設(shè)置為不可改
?? ??? ?this.setVisible(true); ?//窗體設(shè)置為可見
?? ??? ?this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);?
?? ??? ?//光標(biāo)聚焦在用戶框中
?? ??? ?jtf1.requestFocus();?
?? ??? ?//為登入按鈕添加監(jiān)聽器
?? ??? ?jbt1.addActionListener(new ActionListener() {?
?? ??? ??? ?@Override
?? ??? ??? ?public void actionPerformed(ActionEvent e) {
?? ??? ??? ??? ?// TODO 自動生成的方法存根
?? ??? ??? ??? ?String name=jtf1.getText();
?? ??? ??? ??? ?String password=new String(jpf2.getPassword()); //獲取密碼框中的密碼
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?if(password.equals("123")) {
?? ??? ??? ??? ??? ??? ?setVisible(false);
?? ??? ??? ??? ??? ??? ?new Client(jtf1.getText());
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else {
?? ??? ??? ??? ??? ??? ?JOptionPane.showConfirmDialog(null, "用戶名或密碼錯誤!",
?? ??? ??? ??? ??? ??? ??? ??? ?"提示",JOptionPane.DEFAULT_OPTION);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?} catch (Exception e2) {
?? ??? ??? ??? ??? ?// TODO: handle exception
?? ??? ??? ??? ??? ?e2.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?});
?? ??? ?jbt2.addActionListener(new ActionListener() { //為取消按鈕添加監(jiān)聽器
?? ??? ??? ?
?? ??? ??? ?@Override
?? ??? ??? ?public void actionPerformed(ActionEvent e) {
?? ??? ??? ??? ?// TODO 自動生成的方法存根
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?setVisible(false);
?? ??? ??? ??? ?} catch (Exception e2) {
?? ??? ??? ??? ??? ?// TODO: handle exception
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?//移動光標(biāo)聚集
?? ?public void actionPerformed(ActionEvent e) {
?? ??? ?Object o=e.getSource();
?? ??? ?if(o==jtf1) {
?? ??? ??? ?jpf2.requestFocus();
?? ??? ?}
?? ?}
}

public class Client extends JFrame ?{
?? ?public DataOutputStream dos=null;
?? ?public DataInputStream dis=null;
?? ?public Socket s=null;
?? ?public ServerSocket sc=null;
?? ?//
?? ?public JTextArea jta=new JTextArea(10,20);
?? ?public JTextField jtf=new JTextField(20);
?? ?public JScrollPane jsp = new JScrollPane(jta);
?? ?static final String CONNSTR="127.0.0.1";
?? ?public String ClientName="";

?? ?//構(gòu)造函數(shù)初始化
?? ?Client(String tClientName) throws IOException{
?? ??? ?ClientName=tClientName;
?? ??? ?this.setTitle("客服端:"+ClientName);
?? ??? ?jta.setEditable(false); //文本顯示框不可編輯
?? ??? ?this.add(jtf,BorderLayout.SOUTH);
?? ??? ?this.add(jsp,BorderLayout.CENTER);
?? ??? ?//默認的設(shè)置是超過文本框才會顯示滾動條,以下設(shè)置讓滾動條一直顯示
?? ??? ?jsp.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
?? ??? ?//連接服務(wù)器
?? ??? ?
?? ??? ?try {
?? ??? ??? ?s=new Socket(CONNSTR,9995);
?? ??? ??? ?new ThreadClient(s,this).start();
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO: handle exception
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?JOptionPane.showConfirmDialog(null, "用戶連接服務(wù)器失敗",
?? ??? ??? ??? ??? ?"提示",JOptionPane.DEFAULT_OPTION);
?? ??? ?}
?? ??? ?//發(fā)送登入信息到服務(wù)器
?? ??? ?String firstStr="\""+ClientName+"\":"+"登入成功";
?? ??? ?DataOutputStream firstdataOutputStream = new DataOutputStream(s.getOutputStream());
?? ??? ?//發(fā)送用戶名到服務(wù)端
?? ??? ?firstdataOutputStream.writeUTF(firstStr);
?? ??? ?firstdataOutputStream.flush();
?? ??? ?//
?? ??? ?this.setBounds(300,300,300,400);?? ??? ?
?? ??? ?//this.setSize(500, 300); ? ?//設(shè)置窗體大小
?? ??? ?setLocationRelativeTo(null); //設(shè)置窗口居中
?? ??? ?this.setVisible(true); ?//窗體設(shè)置為可見
?? ??? ?this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);?
?? ??? ?jtf.requestFocus();
?? ??? ?//
?? ??? ?
?? ??? ?jtf.addActionListener(new ActionListener() {
?? ??? ??? ?@Override
?? ??? ??? ?public void actionPerformed(ActionEvent e) {
?? ??? ??? ??? ?// TODO 自動生成的方法存根
?? ??? ??? ??? ?String str=jtf.getText();
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?if(str.length()>0) {
?? ??? ??? ??? ??? ??? ?str="\""+ClientName+"\":"+str;
?? ??? ??? ??? ??? ??? ?sendMessage(str, s);
?? ??? ??? ??? ??? ??? ?jtf.setText("");
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?} catch (Exception e2) {
?? ??? ??? ??? ??? ?// TODO: handle exception
?? ??? ??? ??? ??? ?e2.printStackTrace();
?? ??? ??? ??? ??? ?str="\""+ClientName+"\":"+"已退出";
?? ??? ??? ??? ??? ?sendMessage(str,s);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?//客服端發(fā)送信息到服務(wù)器
?? ?protected void sendMessage(String message, Socket s) {
?? ??? ?try {
?? ??? ??? ?DataOutputStream dos = new DataOutputStream(s.getOutputStream());
?? ??? ??? ?dos.writeUTF(message);
?? ??? ??? ?dos.flush();?? ?
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO: handle exception
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
?? ?public static void main(String[] args) {
?? ??? ?new Login();
?? ?}
}
//定義線程類,讀取服務(wù)器發(fā)送的信息
class ThreadClient extends Thread {
?? ?private Socket s;
?? ?private Client clientChat;

?? ?ThreadClient(Socket socket, Client clientChat) {
?? ??? ?this.s = socket;
?? ??? ?this.clientChat = clientChat;
?? ?}
?? ?@Override
?? ?public void run() {
?? ??? ?String message;
?? ??? ?try {
?? ??? ??? ?while (true) {
?? ??? ??? ??? ??? ?DataInputStream DataInputStream = new DataInputStream(s.getInputStream());
?? ??? ??? ??? ??? ?message = DataInputStream.readUTF();
?? ??? ??? ??? ??? ?clientChat.jtf.setText("");
?? ??? ??? ??? ??? ?clientChat.jta.append(message+"\n");
?? ??? ??? ??? ?}
?? ??? ?}
?? ??? ?catch (IOException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?message="\""+clientChat.ClientName+"\":"+"已退出";
?? ??? ??? ??? ?clientChat.sendMessage(message,s);
?? ??? ?}
?? ?}
}

服務(wù)端

import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import javax.naming.ldap.LdapName;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Server extends JFrame {
?? ?//static List<Socket>ls=new ArrayList<Socket>();
?? ?//static List<String>lname=new ArrayList<String>();
?? ?public static Map<Socket,String> socketsMaps?
?? ?= Collections.synchronizedMap(new HashMap<Socket,String>());
?? ?//
?? ?ServerSocket sc=null;
?? ?Socket s=null;
?? ?public JTextArea jta=new JTextArea(10,20);
?? ?public JScrollPane jsp=new JScrollPane(jta);
?? ?public String ServerName="服務(wù)器";
?? ?static int number=1;
?? ?Server() throws IOException{
?? ??? ?super();
?? ??? ?setTitle("服務(wù)器");
?? ??? ?jta.setEditable(false); //文本顯示框不可編輯
?? ??? ?this.add(jsp,BorderLayout.CENTER);
?? ??? ? ?//默認的設(shè)置是超過文本框才會顯示滾動條,以下設(shè)置讓滾動條一直顯示
?? ??? ?jsp.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
?? ??? ?//this.add(jta,BorderLayout.CENTER); ?//不需要重復(fù)添加
?? ??? ?this.setBounds(300,300,300,400);
?? ??? ?this.setVisible(true);
?? ??? ?this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
?? ??? ?//
?? ?}
?? ?public void init() {
?? ??? ?int count = 0; // 記錄登錄到該服務(wù)器的客戶端個數(shù)
?? ??? ??? ?try {
?? ??? ??? ??? ?sc = new ServerSocket(9995); // 創(chuàng)建一個ServerSocket對象,端口號為1906
?? ??? ??? ??? ?jta.append("服務(wù)器已啟動"+'\n');
?? ??? ??? ??? ?while (true) {
?? ??? ??? ??? ??? ?Socket socket=sc.accept();
?? ??? ??? ??? ??? ?new ThreadServer(socket,this).start();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?catch (Exception e) {
?? ??? ??? ??? ?// TODO: handle exception
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ??? ?
?? ?}
?? ?
?? ?public static void main(String[] args) throws IOException{
?? ??? ?new Server().init();
?? ?}
}

class ThreadServer extends Thread {
?? ?private Socket ts;
?? ?private Server tsr;
?? ?ThreadServer(){};
?? ?ThreadServer(Socket s,Server sr)
?? ?{
?? ??? ?this.ts = s;
?? ??? ?this.tsr=sr;
?? ?}
?? ?public void run() {
?? ??? ?
?? ??? ?try {
?? ??? ??? ?while(true)
?? ??? ??? ?{
?? ??? ??? ??? ?DataInputStream dis = new DataInputStream(ts.getInputStream());
?? ??? ??? ??? ?String message=dis.readUTF();
?? ??? ??? ??? ?if(message.endsWith(":登入成功")) {
?? ??? ??? ??? ??? ?message.replaceAll(":登入成功", "");
?? ??? ??? ??? ??? ?tsr.socketsMaps.put(ts,message);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?tsr.jta.append(message+'\n');
?? ??? ??? ??? ?Set<Socket> sockets = tsr.socketsMaps.keySet();
?? ??? ??? ??? ?for(Socket tts : sockets) {
?? ??? ??? ??? ??? ?DataOutputStream dos = new DataOutputStream(tts.getOutputStream());
?? ??? ??? ??? ??? ?dos.writeUTF(message);
?? ??? ??? ??? ??? ?dos.flush();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?catch (IOException e2) {
?? ??? ??? ?// TODO: handle exception
?? ??? ??? ?e2.printStackTrace();
?? ??? ?}
?? ?}
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring-boot使用AOP統(tǒng)一處理日志

    spring-boot使用AOP統(tǒng)一處理日志

    這篇文章主要為大家詳細介紹了spring-boot使用AOP統(tǒng)一處理日志,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • WeakHashMap?和?HashMap?區(qū)別及使用場景

    WeakHashMap?和?HashMap?區(qū)別及使用場景

    這篇文章主要為大家介紹了WeakHashMap?和?HashMap?的區(qū)別是什么以及何時使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Java?Swing實現(xiàn)掃雷源碼

    Java?Swing實現(xiàn)掃雷源碼

    這篇文章主要為大家詳細介紹了Java?Swing實現(xiàn)掃雷源碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • SpringDataJPA之Specification復(fù)雜查詢實戰(zhàn)

    SpringDataJPA之Specification復(fù)雜查詢實戰(zhàn)

    這篇文章主要介紹了SpringDataJPA之Specification復(fù)雜查詢實戰(zhàn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java線程組與未處理異常實例分析

    Java線程組與未處理異常實例分析

    這篇文章主要介紹了Java線程組與未處理異常,結(jié)合實例形式分析了java線程組處理異常的相關(guān)技巧與操作注意事項,需要的朋友可以參考下
    2019-09-09
  • Java MyBatis 多表查詢詳解

    Java MyBatis 多表查詢詳解

    這篇文章主要給大家介紹了關(guān)于MyBatis如何實現(xiàn)多表查詢(多對一、一對多)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)
    2021-09-09
  • Java JDK8新增Optional工具類講解

    Java JDK8新增Optional工具類講解

    這篇文章主要介紹了Java JDK8新增Optional工具類講解,本文通過老版和jdk8對比對null的處理方式,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • java如何獲取本機IP地址

    java如何獲取本機IP地址

    這篇文章主要為大家詳細介紹了java如何獲取本機IP地址,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Java線程池隊列LinkedTransferQueue示例詳解

    Java線程池隊列LinkedTransferQueue示例詳解

    這篇文章主要為大家介紹了Java線程池隊列LinkedTransferQueue示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Feign 集成 Hystrix實現(xiàn)不同的調(diào)用接口不同的設(shè)置方式

    Feign 集成 Hystrix實現(xiàn)不同的調(diào)用接口不同的設(shè)置方式

    這篇文章主要介紹了Feign 集成 Hystrix實現(xiàn)不同的調(diào)用接口不同的設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評論