java線上聊天項目1.3版設計好友列表框功能補充,因只要用戶登錄就發送一串新列表,導致不同客戶端好友列表不同問題

来源:http://www.cnblogs.com/qingyundian/archive/2017/12/23/8088079.html
-Advertisement-
Play Games

解決完畢後效果圖: 好友列表Vector添加的時候進行判斷,如果有相同的則不添加 int flag=0; for (int i = 0; i < names.size(); i++) { if (name.equals(names.get(i))) { flag=1; } } if(flag==0) ...


解決完畢後效果圖:

 

好友列表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() {
            @Override
            public void windowClosing(WindowEvent e) {
                disconnect();
                System.exit(0);
            }
        });

        // 調用傻傻的等待接收列表信息
        new Thread(new WaitingReceive()).start();
        // 雙擊激活聊天對話框
        list.addMouseListener(new MouseAdapter() {
            @Override
            public 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 {

        @Override
        public 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());
            }
        });
    }
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • js中的不同的數據類型之間的比較轉換規則如下: 1. 對象和布爾值比較 對象和布爾值進行比較時,對象先轉換為字元串,然後再轉換為數字,布爾值直接轉換為數字 2. 對象和字元串比較 對象和字元串進行比較時,對象轉換為字元串,然後兩者進行比較。 3. 對象和數字比較 對象和數字進行比較時,對象先轉換為字 ...
  • 本文分為三個部分 一、JS數字精度丟失的一些典型問題 1. 兩個簡單的浮點數相加 1 0.1 + 0.2 != 0.3 // true 1 0.1 + 0.2 != 0.3 // true 1 0.1 + 0.2 != 0.3 // true 1 0.1 + 0.2 != 0.3 // true 0 ...
  • Angular 是 Google 親兒子,React 是 Facebook 小正太,那咱為啥偏偏選擇了 Vue 下手,一句話,Vue 是咱見過的最對脾氣的 MVVM 框架。之前也使用過 knockout,angular,react 這些框架,但都沒有讓咱產生 follow 的衝動。直到見到 Vue,... ...
  • 1.基礎知識點 1.1 webpack 是一個現代 JavaScript 應用程式的模塊打包器(module bundler)。當 webpack 處理應用程式時,它會遞歸地構建一個依賴關係圖(dependency graph),其中包含應用程式需要的每個模塊,然後將所有這些模塊打包成一個或多個 b ...
  • 統介紹: 1.系統採用主流的 SSM 框架 jsp JSTL bootstrap html5 (PC瀏覽器使用) 2.springmvc +spring4.3.7+ mybaits3.3 SSM 普通java web(非maven, 附贈pom.xml文件) 資料庫:mysql 3.開發工具:mye ...
  • 一、模板方法定義 定義一個操作中的某種演算法的框架,而將一些步驟延遲到子類中。模板方法模式使得子類在不改變一個演算法結構的前提下,對這些步驟進行個性化定義。 二、模板方法程式構成 在模板方法設計模式中,存在一個父類,其中包括兩類方法:模板方法和步驟方法。 模板方法,即實現某種演算法的方法步驟。而這些步驟都 ...
  • 轉載自:http://blog.csdn.net/xu__cg/article/details/53024490 抽象構件 具體構建 裝飾角色 具體裝飾角色 客戶端 ...
  • Spring是一個開源框架,Spring是於2003 年興起的一個輕量級的Java 開發框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中闡述的部分理念和原型衍生而來。 它是為瞭解決企業應用開發的複雜性而創建的。框架的 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...