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

相关文章

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Java并发编程之如何优雅关闭钩子Shutdown Hook

《Java并发编程之如何优雅关闭钩子ShutdownHook》这篇文章主要为大家详细介绍了Java如何实现优雅关闭钩子ShutdownHook,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 目录关闭钩子简介关闭钩子应用场景数据库连接实战演示使用关闭钩子的注意事项开源框架中的关闭钩子机制1.

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B