java在线聊天项目1.3版设计好友列表框功能补充,因只要用户登录就发送一串新列表,导致不同客户端好友列表不同问题

本文主要是介绍java在线聊天项目1.3版设计好友列表框功能补充,因只要用户登录就发送一串新列表,导致不同客户端好友列表不同问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

解决完毕后效果图:

 

好友列表Vector添加的时候进行判断,如果有相同的则不添加

int flag=0;
for (int i = 0; i < names.size(); i++) {
if (name.equals(names.get(i))) {
flag=1;
}
}
if(flag==0) {
names.add(name);
}

好友列表窗代码如下:

package com.swift.frame;import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;public class FriendsFrame extends JFrame {private static final long serialVersionUID = 1L;private Socket s;private DataOutputStream dos;private DataInputStream dis;private boolean connected = false;Vector<String> names = new Vector<String>();JList<String> list = null;public FriendsFrame(String name, Socket socket) {super("欢迎 " + ":" + socket.getLocalPort());this.s = socket;connected = true;names.add("登录用户");try {this.dos = new DataOutputStream(s.getOutputStream());this.dis = new DataInputStream(s.getInputStream());} catch (IOException e) {e.printStackTrace();}JFrame.setDefaultLookAndFeelDecorated(true);JDialog.setDefaultLookAndFeelDecorated(true);try {UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");} catch (ClassNotFoundException e1) {e1.printStackTrace();} catch (InstantiationException e1) {e1.printStackTrace();} catch (IllegalAccessException e1) {e1.printStackTrace();} catch (UnsupportedLookAndFeelException e1) {e1.printStackTrace();}setBounds(100, 100, 247, 581);setVisible(true);final JPanel panel = new JPanel();panel.setLayout(new BorderLayout());getContentPane().add(panel, BorderLayout.NORTH);final JLabel label = new JLabel(new ImageIcon("Images/logo.jpg"));label.setText("New JLabel");panel.add(label, BorderLayout.WEST);label.setPreferredSize(new Dimension(74, 74));final JPanel panel_1 = new JPanel();panel_1.setLayout(new BorderLayout());panel.add(panel_1, BorderLayout.CENTER);final JLabel advancingSwiftLabel = new JLabel();advancingSwiftLabel.setText(name);panel_1.add(advancingSwiftLabel, BorderLayout.CENTER);final JLabel neverWasterLabel = new JLabel();neverWasterLabel.setText("Never waste time any more");panel_1.add(neverWasterLabel, BorderLayout.SOUTH);final JPanel panel_2 = new JPanel();panel_2.setLayout(new BorderLayout());getContentPane().add(panel_2, BorderLayout.SOUTH);final JPanel panel_3 = new JPanel();final FlowLayout flowLayout = new FlowLayout();flowLayout.setAlignment(FlowLayout.LEFT);panel_3.setLayout(flowLayout);panel_2.add(panel_3);final JButton button = new JButton();panel_3.add(button);button.setHorizontalTextPosition(SwingConstants.LEFT);button.setHorizontalAlignment(SwingConstants.LEFT);button.setText("设置");final JButton button_1 = new JButton();panel_3.add(button_1);button_1.setText("查找");final JPanel panel_4 = new JPanel();panel_2.add(panel_4, BorderLayout.EAST);final JButton button_2 = new JButton();panel_4.add(button_2);button_2.setText("退出");final JTabbedPane tabbedPane = new JTabbedPane();getContentPane().add(tabbedPane, BorderLayout.CENTER);final JPanel panel_5 = new JPanel();tabbedPane.addTab("好友列表", null, panel_5, null);list = new JList<String>();panel_5.add(list);final JPanel panel_6 = new JPanel();tabbedPane.addTab("群聊", null, panel_6, null);final JPanel panel_7 = new JPanel();tabbedPane.addTab("聊天记录", null, panel_7, null);this.setDefaultCloseOperation(EXIT_ON_CLOSE);final JMenuBar menuBar = new JMenuBar();setJMenuBar(menuBar);final JMenu menu = new JMenu();menu.setText("操作");menuBar.add(menu);final JMenuItem newItemMenuItem = new JMenuItem();newItemMenuItem.setText("设置");menu.add(newItemMenuItem);final JMenuItem newItemMenuItem_1 = new JMenuItem();newItemMenuItem_1.setText("空间");menu.add(newItemMenuItem_1);final JMenuItem newItemMenuItem_2 = new JMenuItem();newItemMenuItem_2.setText("邮箱");menu.add(newItemMenuItem_2);final JMenu menu_1 = new JMenu();menu_1.setText("会员");menu.add(menu_1);final JMenuItem newItemMenuItem_3 = new JMenuItem();newItemMenuItem_3.setText("会员官网");menu_1.add(newItemMenuItem_3);final JMenuItem newItemMenuItem_4 = new JMenuItem();newItemMenuItem_4.setText("会员专区");menu_1.add(newItemMenuItem_4);menu.addSeparator();final JMenu menu_2 = new JMenu();menu_2.setText("安全");menu.add(menu_2);final JMenuItem newItemMenuItem_5 = new JMenuItem();newItemMenuItem_5.setText("紧急冻结");menu_2.add(newItemMenuItem_5);final JMenuItem newItemMenuItem_6 = new JMenuItem();newItemMenuItem_6.setText("密码保护");menu_2.add(newItemMenuItem_6);final JMenuItem newItemMenuItem_7 = new JMenuItem();newItemMenuItem_7.setText("退出");menu.add(newItemMenuItem_7);final FlowLayout flowLayout_1 = new FlowLayout();flowLayout_1.setAlignment(FlowLayout.RIGHT);this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {disconnect();System.exit(0);}});// 调用傻傻的等待接收列表信息new Thread(new WaitingReceive()).start();// 双击激活聊天对话框list.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {if (e.getClickCount() == 2) {new ChatFrame(s);}}});}public void disconnect() {try {if (dos != null)dos.close();if (dis != null)dis.close();if (s != null)s.close();} catch (IOException e) {e.printStackTrace();}}class WaitingReceive implements Runnable {@Overridepublic void run() {try {while (connected) {String name = dis.readUTF();System.out.println(name);int flag=0;for (int i = 0; i < names.size(); i++) {if (name.equals(names.get(i))) {flag=1;}}if(flag==0) {names.add(name);}list.setListData(names);}} catch (SocketException e) {System.out.println("a client has been closed!");} catch (IOException e) {e.printStackTrace();}}}/*** WindowBuilder generated method.<br>* Please don't remove this method or its invocations.<br>* It used by WindowBuilder to associate the {@link javax.swing.JPopupMenu} with* parent.*/private static void addPopup(Component component, final JPopupMenu popup) {component.addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent e) {if (e.isPopupTrigger())showMenu(e);}public void mouseReleased(MouseEvent e) {if (e.isPopupTrigger())showMenu(e);}private void showMenu(MouseEvent e) {popup.show(e.getComponent(), e.getX(), e.getY());}});}
}

 

这篇关于java在线聊天项目1.3版设计好友列表框功能补充,因只要用户登录就发送一串新列表,导致不同客户端好友列表不同问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/762189

相关文章

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

SpringBoot日志级别与日志分组详解

《SpringBoot日志级别与日志分组详解》文章介绍了日志级别(ALL至OFF)及其作用,说明SpringBoot默认日志级别为INFO,可通过application.properties调整全局或... 目录日志级别1、级别内容2、调整日志级别调整默认日志级别调整指定类的日志级别项目开发过程中,利用日志