2021/9/1

这篇博客展示了如何使用Java实现一个QQ群管理应用,包括群创建界面的UI设计,搜索、添加群成员的功能,以及服务器端处理群添加请求的方法。用户可以搜索并添加群,服务器根据输入查询群信息并返回给客户端。应用还实现了双击群友请求确认的交互功能。
摘要由CSDN通过智能技术生成

#8:30-9:00 签到打卡

#9:30-12:00 观看java视频

#14:00-18:00 写qq项目

1.群创建界面类

package qqc.view;

import qqc.massage.Massage;
import qqc.mode.Mthreads;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class qqaddqun  extends JFrame implements ActionListener, MouseListener {
    public String myname;
    public String id;
    JTextField jt;
    JButton jb,jb1[]=new JButton[2];
    JPanel jp,jp1;
    JTextField jl[]=new JTextField[2];
    boolean flag[]=new boolean[2];
    public String[] oid=new String[2];
    public String[] oname=new String[2];
    public String[] friendid=new String[10];
    public String[] friendname=new String[10];
    int i=0,j=1;
    JLabel[] jll=new JLabel[10];
    public qqaddqun(String id,String myname){
        flag[0]=flag[1]=false;
        this.myname=myname;
        this.id=id;
        this.setTitle(myname);
        jb=new JButton("搜索");
        jb.addActionListener(this);
        jt=new JTextField(15);
        jp=new JPanel(new GridLayout(70,1,4,4));
        jp1=new JPanel();
        jp1.add(jt);
        jp1.add(jb);
        jb1=new JButton[2];
        jl[0]=new JTextField();
        jl[1]=new JTextField();
        jb1[0]=new JButton("添加");
        jb1[0].addActionListener(this);
        jb1[1]=new JButton("添加");
        jb1[1].addActionListener(this);
        jp.add(jl[0]);
        jp.add(jb1[0]);
        jp.add(jl[1]);
        jp.add(jb1[1]);
        this.add(jp1,BorderLayout.NORTH);
        this.add(new JScrollPane(jp),BorderLayout.CENTER);
        this.setSize(300,400);
}
    public static void main(String[] args) {
        new qqaddqun("1","流连");
    }
    public void shows(String id,String name){
        oid[i]=id;
        oname[i]=name;
        jl[i].setText("群号:"+id+"   群昵称:"+name);
        flag[i]=true;
        i++;
    }
    public void shows(){
        if(i==0){
            jl[i].setText("很抱歉没有找到当前id的群号");
        }else{
            jl[i].setText("很抱歉没有找到当前名字的群号");
        }
        flag[i]=false;
        i++;
    }
    public void showss(String id,String name){
        jll[j]=new JLabel("群号为"+id+"  昵称为"+name+" 请求加加入此群");
        jp.add(jll[j]);
        friendid[j]=id;
        friendname[j]=name;
        jll[j].addMouseListener(this);
        j++;
        jp.updateUI();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==jb){
            try {
                i=0;
                ObjectOutputStream oos=new ObjectOutputStream(Mthreads.getThread(id).s.getOutputStream());
                Massage m=new Massage();
                m.setS("33");
                m.setMyid(id);
                m.setXiaoxi(jt.getText().trim());
                oos.writeObject(m);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }else if(e.getSource()==jb1[0]){
            if(flag[0]){
                int n=JOptionPane.showConfirmDialog(null,"你确定添加此群吗?","添加提示",JOptionPane.YES_NO_OPTION);
                if(n==0){
                    try {
                        ObjectOutputStream oos=new ObjectOutputStream(Mthreads.getThread(id).s.getOutputStream());
                        Massage m=new Massage();
                        m.setS("34");
                        m.setMyid(id);
                        m.setMyname(myname);
                        m.setOid(oid[0]);
                        m.setOname(oname[0]);
                        oos.writeObject(m);
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }
            }
        }else if(e.getSource()==jb1[1]){
            if(flag[1]){
                int n=JOptionPane.showConfirmDialog(null,"你确定添加此群吗?","添加提示",JOptionPane.YES_NO_OPTION);
                if(n==0){
                    try {
                        ObjectOutputStream oos=new ObjectOutputStream(Mthreads.getThread(id).s.getOutputStream());
                        Massage m=new Massage();
                        m.setS("22");
                        m.setMyid(id);
                        m.setMyname(myname);
                        m.setOid(oid[1]);
                        m.setOname(oname[1]);
                        oos.writeObject(m);
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if(e.getClickCount()==2){
            int n=JOptionPane.showConfirmDialog(null,"你同意添加他群友吗?","添加提示",JOptionPane.YES_NO_OPTION);
            if(n==0){
                for(int i=0;i<10;i++){
                    if (jll[i]==e.getSource()) {
                        try {
                            ObjectOutputStream oos=new ObjectOutputStream(Mthreads.getThread(id).s.getOutputStream());
                            Massage m=new Massage();
                            m.setS("23");
                            m.setMyid(id);
                            m.setMyname(myname);
                            m.setOid(friendid[i]);
                            m.setOname(friendname[i]);
                            oos.writeObject(m);
                        } catch (IOException ioException) {
                            ioException.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

2.服务器添加群方法

                }else if(m.getS().equals("33")){
                    oos=new ObjectOutputStream(s.getOutputStream());
                    Mysql my=new Mysql();
                    String ss=my.qun("2",m.getXiaoxi());
                    if(ss!=null){
                        m.setS("33");
                        m.setOname(ss);
                        m.setOid(m.getXiaoxi());
                        oos.writeObject(m);}
                    else{
                        m.setS("34");
                        oos.writeObject(m);
                    }
                    oos=new ObjectOutputStream(s.getOutputStream());
                    ss=my.seclectall("4","name = '"+m.getXiaoxi()+"'");
                    if(ss!=null){
                        m.setS("33");
                        m.setOname(m.getXiaoxi());
                        m.setOid(ss);
                        oos.writeObject(m);}
                    else {
                        m.setS("34");
                        oos.writeObject(m);
                    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值