⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainwin.java

📁 1. 服务器端:(Windows平台)首先应有java环境jdk,建议采用jdk1.3。然后应有一个数据库系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

  }
  /**Overridden so we can exit when window is closed*/
  protected void processWindowEvent(WindowEvent e) {//关闭程序
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
     //tell who add me as friend offline
     try{
       String whoips;
       String  s="offline"+myjicq;
       s.trim();
       System.out.println(s);
      byte[] data=s.getBytes();
for(int i=0;i<whoaddmesip.size();i++){
     whoips=whoaddmesip.get(i).toString().trim();
        sendPacket=new
        DatagramPacket(data,s.length(),InetAddress.getByName(whoips),sendPort);
             sendSocket.send(sendPacket);//通知好友我下线了
}//for
          } catch(IOException e2){sendtext.append(sendtext.getText());
                 e2.printStackTrace();}
//end offline

//告诉服务器我下线了
     out.println("logout");
     out.println(myjicq);
     //socket.close();
      System.exit(0);

    }
  }
  void this_mousePressed(MouseEvent e) {
 jButton1.setIcon(icon1);
  }

  void list_mouseClicked(MouseEvent e) {
jPopupMenu1.show(this,e.getX()+20,e.getY()+20);
  }

  void direct_mouseClicked(MouseEvent e) {//直接添加好友
 DirectAdd.setLocationRelativeTo(MainWin.this);
 DirectAdd.setSize(260,160);
DirectAdd.show();
// JOptionPane.showMessageDialog(this,":-(对不起,还有好多没做出来.hg","ok",JOptionPane.INFORMATION_MESSAGE);

  }

  void ok_mouseClicked(MouseEvent e) {
about.dispose();
  }



  void cancel_mouseClicked(MouseEvent e) {
senddata.dispose();
  }

  void sendmessage_mousePressed(MouseEvent e) {//发消息菜单
senddata.setLocationRelativeTo(MainWin.this);
senddata.setBounds(e.getX()+50,e.getY()+50,400,280);
index=list.getSelectedIndex();
System.out.println(index);
nametext.setText(friendnames.get(index).toString());
icqno.setText(friendjicq.get(index).toString());
 theip=friendips.get(index).toString();//ip address
System.out.println(theip);
  senddata.show();

  }

  void find_mouseClicked(MouseEvent e) {//显示查找好友窗口

findf.show();
}//find

  void send_mouseClicked(MouseEvent e) {//发送消息
//*********send message
    try{
       String s=sendtext.getText().trim();
      // System.out.println(s);
      byte[] data=s.getBytes();
      System.out.println(theip);
      theip.trim();
       if (theip.equals("null")||theip.equals(" ")||theip.equals("0")){JOptionPane.showMessageDialog(this,":-(对不起,不在线","ok",JOptionPane.INFORMATION_MESSAGE);
                                }

      else{
       sendPacket=new
        DatagramPacket(data,s.length(),InetAddress.getByName(theip),sendPort);
             sendSocket.send(sendPacket);}

          } catch(IOException e2){sendtext.append(sendtext.getText());
                 e2.printStackTrace();}
        senddata.dispose();
//*******end send message
  }

  void getmessage_mousePressed(MouseEvent e) {//接受消息菜单
String message=received.trim();
  index=list.getSelectedIndex();
if(index==index4)getinfo.append(message);
else getinfo.append(" ");
getfromname.setText(friendnames.get(index).toString().trim());
getfromjicq.setText(friendjicq.get(index).toString().trim());
getdata.show();
  }

  void getok_mouseClicked(MouseEvent e) {//接受消息 
  getinfo.setText(" ");
getdata.dispose();
received=" ";
  }
//update friend info;
  void update_mouseClicked(MouseEvent e) {//更新好友列表
tempname=findf.tmpname;
tempjicq=findf.tmpjicq;
tempip=findf.tmpip;
temppic=findf.tmppic;
tempstatus=findf.tmpstatus;
tempemail=findf.tmpemail;
tempinfo=findf.tmpinfo;
DefaultListModel mm2=(DefaultListModel)list.getModel();
int picid=0;
for(int p=0;p<tempname.size();p++){
 picid=Integer.parseInt(temppic.get(p).toString());
if(status.get(p).equals("1")){
     mm2.addElement(new Object[]{tempname.get(p),new ImageIcon(picsonline[picid])});}
else {
 mm2.addElement(new Object[]{tempname.get(p),new ImageIcon(picsonline[picid])});}
//picid=Integer.parseInt(temppic.get(p).toString());
//mm2.addElement(new Object[]{tempname.get(p),new ImageIcon(picsonline[picid])});

     }//for
//add to friendlist
for(int k=0;k<tempname.size();k++){
friendnames.add(tempname.get(k));
friendjicq.add(tempjicq.get(k));
friendips.add(tempip.get(k));
picno.add(temppic.get(k));
status.add(tempstatus.get(k));
friendemail.add(tempemail.get(k));
friendinfo.add(tempinfo.get(k));
}//for
//clean tmp
for(int p=0;p<tempname.size();p++){
findf.tmpip.removeAllElements();
findf.tmpjicq.removeAllElements();
findf.tmpname.removeAllElements();
findf.tmppic.removeAllElements();
findf.tmpstatus.removeAllElements();
findf.tmpemail.removeAllElements();
findf.tmpinfo.removeAllElements();
}
}
//delete freind
  void delfriend_mousePressed(MouseEvent e) {//删除好友
out.println("delfriend");
 int index2;
  index2=list.getSelectedIndex();

out.println(friendjicq.get(index2));//the friendjicq to del
out.println(myjicq);//my jicqno
 DefaultListModel mm=(DefaultListModel)list.getModel();
  mm.removeElementAt(index2);
friendnames.removeElementAt(index2);
  friendips.removeElementAt(index2);
  friendjicq.removeElementAt(index2);
  picno.removeElementAt(index2);
  status.removeElementAt(index2);
  friendemail.removeElementAt(index2);
  friendinfo.removeElementAt(index2);
  }//////////////delfriend
//tell friend i am online
  void online_mouseClicked(MouseEvent e) {
out.println("getwhoaddme");
out.println(myjicq);

   String whoip=" ";
   do{
   try{whoip=in.readLine().trim();
    if(whoip.equals("over"))  break;
    whoaddmesip.add(whoip);
    }catch(IOException s){System.out.println("false getwhoaddme");}
       }while(!whoip.equals("over"));
for(int i=0;i<whoaddmesip.size();i++)
{System.out.println(whoaddmesip.get(i));
}
 try{
       String whoips;
       String  s="online"+myjicq;
       s.trim();
       System.out.println(s);
      byte[] data=s.getBytes();
for(int i=0;i<whoaddmesip.size();i++){
     whoips=whoaddmesip.get(i).toString().trim();
        sendPacket=new
        DatagramPacket(data,s.length(),InetAddress.getByName(whoips),sendPort);
             sendSocket.send(sendPacket);
}//for
          } catch(IOException e2){sendtext.append(sendtext.getText());
                 e2.printStackTrace();System.exit(1);}

}/////end tellfrienonline

  void myinfo_mouseClicked(MouseEvent e) {//陌生人消息 
if(fromunknow){
String message=received.trim();
getinfo.append(message);
getdata.show();
}

  }

  void lookinfo_mousePressed(MouseEvent e) {//查看好友资料菜单
hello.setLocationRelativeTo(MainWin.this);
hello.setBounds(e.getX()+50,e.getY()+50,380,300);
  index=list.getSelectedIndex();
helloname.setText(friendnames.get(index).toString());
hellojicq.setText(friendjicq.get(index).toString());
helloemail.setText(friendemail.get(index).toString());
helloinfo.setText(friendinfo.get(index).toString().trim());
hello.show();
  }

  void hellook_mouseClicked(MouseEvent e) {//查看好友资料关闭
hello.dispose();
  }
//add the one who add me as friend
  void addit_mouseClicked(MouseEvent e) {
//如果有人加我,我就加它
 out.println("addnewfriend");
out.println(tempgetjicq);
out.println(myjicq);
 String thename=" ";
try{
        String thejicqno,theip,thestatus,picinfo,email,infos;
        do{thename=in.readLine();
            if(thename.equals("over"))  break;
            friendnames.add(thename);
           thejicqno=in.readLine();
            friendjicq.add(new Integer(thejicqno));
           theip=in.readLine();
            friendips.add(theip);
         thestatus=in.readLine();
          status.add(thestatus);
          picinfo=in.readLine();
          picno.add(new Integer(picinfo));
          email=in.readLine();
          friendemail.add(email);
          infos=in.readLine();
         friendinfo.add(infos);
          }while(!thename.equals("over"));
 }catch(IOException e1){System.out.println("false");}
int dddd=friendnames.size()-1;
DefaultListModel mm2=(DefaultListModel)list.getModel();
int picid;
picid=Integer.parseInt(picno.get(dddd).toString());
mm2.addElement(new Object[]{friendnames.get(dddd),new ImageIcon(picsonline[picid])});
  }

  void iknow_mouseClicked(MouseEvent e) {
OneAddyou.dispose();
  }

  void directaddok_mouseClicked(MouseEvent e) {//直接添加好友
 out.println("addnewfriend");
out.println(friendid.getText().trim());
out.println(myjicq);
 String thename=" ";
try{
        String thejicqno,theip,thestatus,picinfo,email,infos;
        do{thename=in.readLine();
            if(thename.equals("over"))  break;
            friendnames.add(thename);
           thejicqno=in.readLine();
            friendjicq.add(new Integer(thejicqno));
           theip=in.readLine();
            friendips.add(theip);
         thestatus=in.readLine();
          status.add(thestatus);
          picinfo=in.readLine();
          picno.add(new Integer(picinfo));
          email=in.readLine();
          friendemail.add(email);
          infos=in.readLine();
         friendinfo.add(infos);
          }while(!thename.equals("over"));
 }catch(IOException e1){System.out.println("false");}
int dddd=friendnames.size()-1;
DefaultListModel mm2=(DefaultListModel)list.getModel();
int picid;
picid=Integer.parseInt(picno.get(dddd).toString());
mm2.addElement(new Object[]{friendnames.get(dddd),new ImageIcon(picsonline[picid])});
 DirectAdd.dispose();
  };//end directadd friend

}//end class MainWin
//以下继承DefaultListModel类以创建好友列表
class NameAndPicListModel extends DefaultListModel
      {   public NameAndPicListModel(Vector friendnames,String [] pics)
          { for(int i=0;i<friendnames.size();++i){
          addElement(new Object[]{friendnames.get(i),new ImageIcon(pics[i])});
          }
          }
  public  String getName(Object object)
  { Object[] array=(Object[])object;
  return(String) array[0];
  }
public Icon getIcon(Object object)
{ Object[] array=(Object[]) object;
  return (Icon) array[1];
  }
}
  class NameAndPicListCellRenderer extends JLabel implements ListCellRenderer
{ private Border lineBorder=BorderFactory.createLineBorder(Color.red,2),
           emptyBorder =BorderFactory.createEmptyBorder(2,2,2,2);
  public NameAndPicListCellRenderer(){
     setOpaque(true);
     }
public Component getListCellRendererComponent(JList list,Object value,int index,
                                                boolean isSelected,boolean cellHasFocus)
      { NameAndPicListModel model=(NameAndPicListModel) list.getModel();
        setText(model.getName(value));
        setIcon(model.getIcon(value));
        if(isSelected){setForeground(list.getSelectionForeground());
                         setBackground(list.getSelectionBackground());
                         }
          else { setForeground(list.getForeground());
                   setBackground(list.getBackground());}
          if(cellHasFocus) setBorder(lineBorder);
                else setBorder(emptyBorder);
          return this;
          }
          }

class MainWin_list_mouseAdapter extends java.awt.event.MouseAdapter {
  MainWin adaptee;
  MainWin_list_mouseAdapter(MainWin adaptee) {
    this.adaptee = adaptee;
  }
  public void mouseClicked(MouseEvent e) {
    adaptee.list_mouseClicked(e);
  }
}

class MainWin_direct_mouseAdapter extends java.awt.event.MouseAdapter {
  MainWin adaptee;

  MainWin_direct_mouseAdapter(MainWin adaptee) {
    this.adaptee = adaptee;
  }
  public void mouseClicked(MouseEvent e) {
    adaptee.direct_mouseClicked(e);
  }
}

class MainWin_ok_mouseAdapter extends java.awt.event.MouseAdapter {
  MainWin adaptee;

  MainWin_ok_mouseAdapter(MainWin adaptee) {
    this.adaptee = adaptee;
  }
  public void mouseClicked(MouseEvent e) {
    adaptee.ok_mouseClicked(e);
  }
}

class MainWin_sendmessage_mouseAdapter extends java.awt.event.MouseAdapter {
  MainWin adaptee;

  MainWin_sendmessage_mouseAdapter(MainWin adaptee) {
    this.adaptee = adaptee;
  }

  public void mousePressed(MouseEvent e) {
    adaptee.sendmessage_mousePressed(e);
  }
}

class MainWin_cancel_mouseAdapter extends java.awt.event.MouseAdapter {
  MainWin adaptee;

  MainWin_cancel_mouseAdapter(MainWin adaptee) {
    this.adaptee = adaptee;
  }
  public void mouseClicked(MouseEvent e) {
    adaptee.cancel_mouseClicked(e);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -