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

相关文章

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

Javaee多线程之进程和线程之间的区别和联系(最新整理)

《Javaee多线程之进程和线程之间的区别和联系(最新整理)》进程是资源分配单位,线程是调度执行单位,共享资源更高效,创建线程五种方式:继承Thread、Runnable接口、匿名类、lambda,r... 目录进程和线程进程线程进程和线程的区别创建线程的五种写法继承Thread,重写run实现Runnab