📄 findfriend2.java~1~
字号:
package cilent;/** * <p>Title: MyQQ</p> * <p>Description: MyQQ</p> * <p>Copyright: Copyright (c) 2007</p> * <p>Company: 南昌大学软件学院 04级网通1班</p> * @author 张利彬 * @version 1.0 */import wodqq.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import java.util.Vector;import java.net.*;import java.io.*;import com.borland.jbcl.layout.*;public class FindFriend2 extends JFrame {//查找好友类 JLabel jLabel1 = new JLabel();ImageIcon img1 = new ImageIcon("src/img/find.png"); BackGroundButton find2 = new BackGroundButton(img1); ImageIcon img2= new ImageIcon("src/img/gb.png"); BackGroundButton jButton3 = new BackGroundButton (img2); JList list2;///////////////////以下是好友的呢称,性别等信息 Vector nickname=new Vector(); Vector sex=new Vector(); Vector place=new Vector(); Vector jicq=new Vector(); Vector ip=new Vector(); Vector pic=new Vector(); Vector status=new Vector(); Vector emails=new Vector(); Vector infos=new Vector();//以下临时保存好友的呢称,性别等信息 Vector tmpjicq=new Vector();//jicqid Vector tmpname=new Vector();//jicqname Vector tmpip=new Vector();//ip Vector tmppic=new Vector();//pic info Vector tmpstatus=new Vector();//status Vector tmpemail=new Vector(); Vector tmpinfo=new Vector();//以下创建网络相关变量 Socket socket; BufferedReader in; PrintWriter out; int myid; String serverhost; int servport; DatagramPacket sendPacket; DatagramSocket sendSocket; int sendPort=5000;////////////////// JPopupMenu findmenu = new JPopupMenu(); JMenuItem look = new JMenuItem(); JMenuItem add = new JMenuItem(); XYLayout xYLayout1 = new XYLayout(); public FindFriend2(int whoami,String host,int port) {//查找好友类构造函数 enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { serverhost=host; servport=port; myid=whoami; jbInit(); } catch(Exception e) { e.printStackTrace(); }//以下与服务器连接 try{socket=new Socket(InetAddress.getByName(serverhost),servport); in=new BufferedReader(new InputStreamReader(socket.getInputStream())); out=new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); sendSocket=new DatagramSocket(); }catch(IOException e1){} } private void jbInit() throws Exception {//以下是程序界面 jLabel1.setFont(new java.awt.Font("DialogInput", 0, 12)); jLabel1.setHorizontalAlignment(SwingConstants.LEADING); jLabel1.setText("上面是在线的朋友"); this.getContentPane().setLayout(xYLayout1); find2.setText("查找"); find2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { find2_actionPerformed(e); } }); find2.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { find2_mouseClicked(e); } }); jButton3.setText("cancel"); jButton3.setRolloverEnabled(false); // nickname=new Vector(); // sex=new Vector(); // place=new Vector(); ListModel model=new FindListModel(nickname,sex,place);//列表模型 ListCellRenderer renderer=new FindListCellRenderer(); list2=new JList(model); list2.setSize(200,200); list2.setBackground(new Color(255, 255, 210)); list2.setAlignmentX((float) 1.0); list2.setAlignmentY((float) 1.0); list2.setCellRenderer(renderer); list2.setVisibleRowCount(7); list2.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(MouseEvent e) { list2_mousePressed(e); } }); look.setText("查看资料"); add.setText("加为好友"); add.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(MouseEvent e) { add_mousePressed(e); } }); this.getContentPane().setBackground(new Color(156, 225, 255)); this.setDefaultCloseOperation(HIDE_ON_CLOSE); this.setEnabled(true); this.setLocale(java.util.Locale.getDefault()); this.setResizable(true); this.setState(Frame.NORMAL); this.setTitle(""); xYLayout1.setWidth(261); xYLayout1.setHeight(162); this.getContentPane().add(new JScrollPane(list2)); findmenu.add(look); findmenu.add(add); this.getContentPane().add(jLabel1, new XYConstraints(7, 132, -1, -1)); this.getContentPane().add(find2, new XYConstraints(108, 135, 75, 21)); this.getContentPane().add(jButton3, new XYConstraints(185, 135, -1, 20)); }//以下是关闭本窗口 protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { //this.dispose(); this.hide(); } }//以下向服务器发送查找好友请求 void find2_mouseClicked(MouseEvent e) {out.println("find");DefaultListModel mm=(DefaultListModel)list2.getModel();/////////////////find friend infotry{String s=" ";//从服务器读取好友信息do{ s=in.readLine(); if(s.equals("over")) break; nickname.add(s); sex.add(in.readLine()); place.add(in.readLine()); ip.add(in.readLine()); emails.add(in.readLine()); infos.add(in.readLine());}while(!s.equals("over"));/////////////end find info//read their jicqnoint theirjicq,picinfo,sta;for(int x=0;x<nickname.size();x++){theirjicq=Integer.parseInt(in.readLine());//System.out.println(theirjicq);jicq.add(new Integer(theirjicq));picinfo=Integer.parseInt(in.readLine());pic.add(new Integer(picinfo));sta=Integer.parseInt(in.readLine());//System.out.println(sta);status.add(new Integer(sta));//System.out.println(jicq.get(x));}//在列表中显示for(int i=0;i<nickname.size();i++){ mm.addElement(new Object[]{nickname.get(i),sex.get(i),place.get(i)}); }//for}catch(IOException e4){System.out.println("false");}}//显示查找好友菜单 void list2_mousePressed(MouseEvent e) {findmenu.show(this,e.getX()+20,e.getY()+50); }/////////////add frined//以下将添加的好友存储在临时矢量 void add_mousePressed(MouseEvent e) {//add friend to databaseint dd;dd=list2.getSelectedIndex();tmpjicq.add(jicq.get(dd));tmpname.add(nickname.get(dd));tmpip.add(ip.get(dd));tmppic.add(pic.get(dd));tmpstatus.add(status.get(dd));tmpemail.add(emails.get(dd));tmpinfo.add(infos.get(dd));//以下向服务器发送添加好友请求out.println("addfriend");out.println(jicq.get(dd));out.println(myid); try{ //以下告诉客户将其加为好友 String whoips; String s="oneaddyou"+myid; s.trim(); System.out.println(s); byte[] data=s.getBytes(); whoips=ip.get(dd).toString().trim(); sendPacket=new DatagramPacket(data,s.length(),InetAddress.getByName(whoips),sendPort); sendSocket.send(sendPacket); } catch(IOException e2){e2.printStackTrace();}//}catch(IOException df){}; } void find2_actionPerformed(ActionEvent e) { }/////////////add friend end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -