导航:首页 > 编程语言 > java添加好友代码

java添加好友代码

发布时间:2024-07-27 07:50:06

『壹』 java如何实现qq加好友功能

思路就是加一张好友表,如表ID,用户id,用户id2,是否为好友;加一个好友,通过Java操作数据库,添加一条数据,并显示出来。

『贰』 微信里有人请求添加好友 来自通讯录好友是什么意思

就是你手机存储了号码,一般这种都是互相认识的,或者对方存了你的号码,通过通讯录就能添加好友

『叁』 急需一个用java 语言写的聊天程序

客户端:
package chatroom;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
public class client extends JFrame implements ActionListener,Runnable{
JPanel conn,text,send;
JLabel name,sendto;
JComboBox list;
JButton con,snd,clear;
JTextArea talk;
JTextField who,say;
jscrollPane jsp;
Socket client;
InputStream is;
OutputStream os;
PrintStream ps;
BufferedReader br;
String receive,yousay;
Thread th;
DataInputStream dis;
Double tof;

client()
{
super("聊天室客户端");
this.setSize(800,600);
this.setResizable(false);
conn=new JPanel();
text=new JPanel();
send=new JPanel();
this.getContentPane().add(conn);
conn.setBounds(0, 0, this.getWidth(),50);
name=new JLabel("姓名:");
who=new JTextField();
con=new JButton("连接");
conn.setLayout(null);
conn.add(name);
name.setBounds(30, 10, 50, 25);
conn.add(who);
who.setBounds(80, 10, 150, 25);
conn.add(con);
con.setBounds(250,10, 60, 25);

this.getContentPane().add(text);
text.setBounds(0,50,this.getWidth(),450);
text.setLayout(new BorderLayout());
jsp=new JScrollPane();
talk=new JTextArea();
jsp.getViewport().setView(talk);
text.add(jsp,"Center");
talk.setLineWrap(true);

this.getContentPane().add(send);
send.setLayout(null);
send.setBounds(0, 480, this.getWidth(), 150);
sendto=new JLabel("发送到:");
snd=new JButton("发送");
list=new JComboBox();
say=new JTextField();
clear=new JButton("清空");
send.add(sendto);
sendto.setBounds(30, 525, 50, 25);
send.add(list);
list.setBounds(100, 525, 75, 25);
send.add(say);
say.setEditable(true);
say.setBounds(200, 525, 300, 25);
send.add(snd);
snd.setBounds(520, 525, 100, 25);
send.add(clear);
clear.setBounds(650, 525, 100, 25);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
con.addActionListener(this);
snd.addActionListener(this);
}

public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("连接"))
{
try
{
client=new Socket(InetAddress.getLocalHost(),55555);
talk.append("连接成功...\n");
con.setText("断开");
is=client.getInputStream();
os=client.getOutputStream();
th=new Thread(this);
String id=who.getText();
byte b[]=id.getBytes();
DataOutputStream dos=new DataOutputStream(os);
int len=b.length;
dos.writeInt(len);
dos.write(b);
th.start();

}catch(Exception e){talk.append("连接失败\n"+e.toString()+"0000");}
}
else if(ae.getSource()==snd)
{
if(list.getSelectedItem().toString()=="所有人")
{
yousay="@open"+say.getText();
byte b[]=yousay.getBytes();
DataOutputStream dos=new DataOutputStream(os);
try{
int len=b.length;
dos.writeInt(len);
dos.write(b);
}catch(Exception e)
{
System.out.print(e.toString()+"1111");
}
}
else
{
yousay="@whisper"+say.getText();
byte b[]=yousay.getBytes();
byte w[]=list.getSelectedItem().toString().getBytes();
DataOutputStream dos=new DataOutputStream(os);
try{
int len=b.length;
int wlen=w.length;
dos.writeInt(len); //内容
dos.write(b);
dos.writeInt(wlen); //发送对象
dos.write(w);
}catch(Exception e)
{
System.out.print(e.toString()+"AAAA");
}
}
}
else if(ae.getActionCommand().equals("断开"))
{
try
{
client.close();
talk.append("连接已断开\n");
con.setText("连接");
}catch(Exception e){System.out.println(e.toString()+"2222");}
}
}
public void run()
{

while(true)
{
try
{
dis=new DataInputStream(is);
tof=dis.readDouble();
if(tof==1.1)
{
dis=new DataInputStream(is);
int number;
list.removeAllItems();
list.addItem("所有人");
number=dis.readInt();
for(int i=0;i<number;i++)
{
int len=dis.readInt();
byte b[]=new byte[len];
dis.read(b);
String name=new String(b);
list.addItem(name);
}

}
else if(tof==2.2)
{
try
{
dis=new DataInputStream(is);
int len=dis.readInt();
byte b[]=new byte[len];
dis.read(b);
receive=new String(b);
talk.append(receive+"\n");
}catch(Exception e){JOptionPane.showMessageDialog(this, "连接已断开","消息",JOptionPane.INFORMATION_MESSAGE);break;}
}
}catch(Exception e){JOptionPane.showMessageDialog(this, "连接已断开","消息",JOptionPane.INFORMATION_MESSAGE);break;}
}
}

public static void main(String[] args) {
client cl=new client();

}

}
服务端:
package chatroom;
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.util.Vector;
import java.io.*;
public class server extends JFrame implements Runnable{
JPanel jp1;
JTextArea jta;
JScrollPane jsp;
Socket socket=null;
ServerSocket server;
InputStream is;
OutputStream os;
static int i=0,login;
int no;
static String s="";
Thread sr;
Thread th[]=new Thread[20];
static Vector ol=new Vector();
static public byte w[]=null;
static String from=""; //人名
static String to="";
static String fromtext=""; //内容
static String totext="";
server()
{
super("聊天室服务端");
this.getContentPane().setLayout(new GridLayout(1,1));
this.setSize(400,300);
jp1=new JPanel();
jta=new JTextArea();
jsp=new JScrollPane();
this.getContentPane().add(jp1);
jp1.setLayout(new GridLayout(1,1));
jsp.getViewport().setView(jta);
jp1.add(jsp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

}
public static void main(String[] args) {
server sr=new server();
sr.sr=new Thread(sr);
sr.sr.start();
sendtoall sta=new sendtoall();
returnfrom rf=new returnfrom(sr);
returnto rt=new returnto(sr);
sta.start();
rf.start();
rt.start();

}
public void run()
{
try
{
server=new ServerSocket(55555);
while(true)
{
socket=server.accept();
is=socket.getInputStream();
DataInputStream dis=new DataInputStream(is);
int len=dis.readInt();
byte b[]=new byte[len];
dis.read(b);
String id=new String(b);
record v=new record(id,socket);
ol.addElement(v);
for (int i=0;i<ol.size();i++)
{
if (ol.elementAt(i).equals(v))
{
no=i;
}
}
login=1;
s=id+"已来到聊天室!";
th[no]=new Thread(new receive(this,no));
th[no].start();
}
}catch(Exception e){System.out.println(e.toString()+"aaaa");}

}
}
class receive implements Runnable
{
InputStream is;
OutputStream os;
server sr;
Socket sk;
int i;
String name;
String ip;
receive(server sr,int i)
{
this.sr=sr;
this.i=i;
sk=((record)sr.ol.elementAt(i)).ip;
name=((record)sr.ol.elementAt(i)).name;
ip=((record)sr.ol.elementAt(i)).ip.getInetAddress().toString();
}
public void run()
{
while(true)
{
try
{
is=sk.getInputStream();
DataInputStream dis=new DataInputStream(is);
int len=dis.readInt();
byte b[]=new byte[len];
dis.read(b);
String abc=new String(b);
sr.jta.append(abc);
if(abc.substring(0,5).equals("@open"))
{

server.s=name+"["+ip.substring(1, ip.length())+"]"+"说:"+abc.substring(5,abc.length());
sr.jta.append(server.s+"\n");
}
else if(abc.substring(0,8).equals("@whisper"))
{
int wlen=dis.readInt();
sr.w=new byte[wlen];
dis.read(sr.w);
server.to=new String(sr.w);
server.from=((record)sr.ol.elementAt(i)).name;
String ip=((record)sr.ol.elementAt(i)).ip.getInetAddress().toString();
// server.s=server.from+"对"+server.to+"["+ip.substring(1, ip.length())+"]"+"悄悄地说:"+abc.substring(8,abc.length());
server.fromtext="你对"+server.to+"["+ip.substring(1, ip.length())+"]"+"悄悄地说:"+abc.substring(8,abc.length());
server.totext=server.from+"["+ip.substring(1, ip.length())+"]"+"对你悄悄地说:"+abc.substring(8,abc.length());
sr.jta.append(server.s+"\n");
}
}catch(Exception e)
{
try
{
DataOutputStream dos=new DataOutputStream(os);
server.ol.removeElementAt(i);
server.s=name+"已离开聊天室.";
server.login=1;
break;
}catch(Exception f){}
}
}
}
}
class sendtoall extends Thread
{
int len,number;
byte b[];
server sr;
Socket st;
OutputStream os;
DataOutputStream dos;
public void run()
{
while(true)
{
try
{
if(server.login==1)
{
number=0;
number=server.ol.size();
dos=new DataOutputStream(os);
for(int i=0;i<server.ol.size();i++)
{
st=((record)sr.ol.elementAt(i)).ip;
os=st.getOutputStream();
dos=new DataOutputStream(os);
dos.writeDouble(1.1);
dos.writeInt(number);
for (int j=0;j<number;j++)
{
String name=((record)sr.ol.elementAt(j)).name;
byte b[]=name.getBytes();
int len=b.length;
dos.writeInt(len);
dos.write(b);
}
}
server.login=0;

}
else if(!server.s.equals("")&&sr.ol.size()>0)
{
b=server.s.getBytes(); //String类型中 汉字占一个字节,但是byte类型中,汉字占两个字节
len=b.length;
//len=server.s.length();
//b=new byte[len];
//b=server.s.getBytes();
for(int i=0;i<server.ol.size();i++)
{
st=((record)sr.ol.elementAt(i)).ip;
os=st.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeDouble(2.2);
dos.writeInt(len);
dos.write(b);
}
server.s="";

}

}catch(Exception e){System.out.println(e.toString()+"sdasd");}
}
}
}
class returnfrom extends Thread
{
int len,wlen;
byte b[];
byte w[];
server sr;
Socket st;
OutputStream os;
DataOutputStream dos;
//String from="",to="";
returnfrom(server sr)
{
this.sr=sr;
}
public void run()
{
while(true)
{
if(!server.fromtext.equals(""))
{
b=server.fromtext.getBytes();
len=b.length;
sr.jta.append(sr.fromtext);
try
{
for(int i=0;i<server.ol.size();i++)
{
if(((record)sr.ol.elementAt(i)).name.equals(server.from))
{
st=((record)sr.ol.elementAt(i)).ip;
os=st.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeDouble(2.2);
dos.writeInt(len);
dos.write(b);
}
}
}catch(Exception e){System.out.println(e.toString()+"wwww");}
server.fromtext="";
server.from="";
}
}

}
}

class returnto extends Thread
{
int len,wlen;
byte b[];
byte w[];
server sr;
Socket st;
OutputStream os;
DataOutputStream dos;
//String from="",to="";
returnto (server sr)
{
this.sr=sr;
}
public void run()
{
while(true)
{
if(!server.totext.equals(""))
{
w=server.totext.getBytes();
wlen=w.length;
try
{
for(int i=0;i<server.ol.size();i++)
{
if(((record)sr.ol.elementAt(i)).name.equals(server.to))
{
st=((record)sr.ol.elementAt(i)).ip;
os=st.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeDouble(2.2);
dos.writeInt(wlen);
dos.write(w);
}
}
}catch(Exception e){System.out.println(e.toString()+"wwww");}
server.totext="";
server.to="";
}
}
}
}

class record
{
String name;
Socket ip;
record(String id,Socket socket)
{
name=id;
ip=socket;
}
}

以前学习java时写的,是个在线聊天室只有群聊,私聊,上下线提示,搞不好里面还有错误- -其他功能自己看着加吧,你要的功能实在是很好很强大.

『肆』 有谁知道qq的好友列表用Java怎么实现有源码参考更好啊,呵呵

首先你要有个存好友的数据结构:friendList
既然是QQ,那么肯定有好友类别了,一个类别底下的子节点为专一个好友。属
然后根据这个数据结构画出这个Jtree,第一列字节点为好友类别,第二列子节点为好友,给这些字节点添加鼠标点击事件,点击后弹出你想要的东西。
最后,在你的接收信息进程内添加一个方法,即一旦有好友上线或者下线则接收改变的好友的信息,之前的存好友的数据结构也随之改变,再根据这个数据结构改变这个JTree,你不会改变的话就重新画一个,tree=new JTree();(tree为你之前的列表),这样就可以了。

阅读全文

与java添加好友代码相关的资料

热点内容
数据线转换器多少钱 浏览:274
美国队长qq皮肤多少钱 浏览:630
win8word文档在哪 浏览:180
甘肃省人事局文件在哪里 浏览:689
spss安装输入代码 浏览:546
网络语言知乎 浏览:596
iphoneicloud无法显示 浏览:112
ict程序包 浏览:729
java有哪些条件语句 浏览:345
冒险岛120级去哪里升级 浏览:511
手机输入法声音文件 浏览:876
下划线哪个app 浏览:48
win10h1z1切换桌面 浏览:911
js定义集合数组 浏览:153
win10企业关闭自动更新 浏览:920
js扩展对象 浏览:370
受控文件的章印内容怎么写 浏览:463
微信云文件丢失 浏览:299
手机bbc文件存在哪个路径 浏览:651
系统安装镜像文件名 浏览:620

友情链接