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

📄 client.java

📁 聊天程序 网络课程设计的报告 JAVA编写的
💻 JAVA
字号:
package myprojects.client;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
class Client extends JFrame implements ActionListener{
	JTextArea msgShow=new JTextArea();
	JTextField name=new JTextField("用户名",10);
	JTextField pwd=new JTextField("密码",10);
	JTextField msg=new JTextField(20);
	JTextField file=new JTextField("要发送的文件路径",20);
	JButton find=new JButton("浏览");
	JButton filesend=new JButton("发送文件");
	JButton connect=new JButton("connecServer");
	JButton send=new JButton("send");
	JButton smile=new JButton("smile");
	JButton setgp=new JButton("群组管理");
	JList list=new JList();
	JList list2=new JList();
	JCheckBox cb=new JCheckBox("私聊",false);
	DefaultListModel dlm;
	DefaultListModel gp;	
	JPanel left=new JPanel();
	JPanel right=new JPanel();
	JPanel top=new JPanel();
	JPanel center=new JPanel();
	JPanel bottom=new JPanel();
	JPanel files=new JPanel();
	Socket sock;
	DataInputStream in;
	DataOutputStream out;
	public Client() {  
      top.add(name);top.add(pwd);top.add(connect);
      dlm=new DefaultListModel();
      gp=new DefaultListModel();
      list=new JList(dlm);
      list2=new JList(gp);
      dlm.addElement("所有");
      gp.addElement("群组");
      list.setSelectedIndex(0);
      list2.setSelectedIndex(0);
      JScrollPane jsp1=new JScrollPane(msgShow);
      JScrollPane jsp2=new JScrollPane(list);
      JScrollPane jsp3=new JScrollPane(list2);
      center.setLayout(new BorderLayout());
      jsp2.setPreferredSize(new Dimension(100,10));
      jsp3.setPreferredSize(new Dimension(100,10));
      center.add(jsp1,"Center");
      center.add(jsp2,"West");
      center.add(jsp3,"East");
      bottom.add(smile,"East");
      bottom.add(cb);
      bottom.add(msg);
      bottom.add(send);
      bottom.add(file);
      bottom.add(find);
      bottom.add(filesend);
      bottom.add(setgp);
      connect.addActionListener(this);
      send.addActionListener(this);
      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(top,"North");
      this.getContentPane().add(center,"Center");
      this.getContentPane().add(bottom,"South");
	  setSize(1024, 300);
      setTitle("Client");
      this.addWindowListener(new WindowAdapter(){
      	public void windowClosing(WindowEvent e)
      	{ try{
      	    out.writeUTF("quit");
      	   }catch(Exception ee){}
      	   dispose();
      	   System.exit(0);	
      	}
       });	  
	  setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
       if(e.getSource()==connect)
       {  if(!name.getText().equals(""))
          {   try{
                 sock=new Socket("127.0.0.1",5555);
                 in=new DataInputStream(sock.getInputStream());
                 out=new DataOutputStream(sock.getOutputStream());
                 out.writeUTF(name.getText());
                 new Say(this,sock);        	
              }catch(Exception ee){
                 JOptionPane.showMessageDialog(null,"连接服务失败!"); 
       	      }
          }
          else if(e.getSource()==send) 
             JOptionPane.showMessageDialog(null,"名字不能为空!"); 
       }
       else
       {   if(!msg.getText().equals(""))
           { String target=(String)list.getSelectedValue();
             if(!cb.isSelected()||target.equals("所有"))
              {
                try{
                 out.writeUTF("public");
       	         out.writeUTF(msg.getText());
       	        }catch(Exception ee){}
       	      }
       	      else if(!cb.isSelected()||!target.equals("所有"))
       	      { try{
                 out.writeUTF("privateall");
                 out.writeUTF(msg.getText());
                 out.writeUTF(target);
       	        }catch(Exception ee){}
       	      }
       	      else
       	      { try{
                 out.writeUTF("private");
                 out.writeUTF(msg.getText());
                 out.writeUTF(target);
       	        }catch(Exception ee){}
       	      }
       	     
       	      
           }
           else 
             JOptionPane.showMessageDialog(null,"信息不能为空!"); 
       }
    }
	public static void main(String args[]) 
	{
		Client mainFrame = new Client();
	}
}
class Say extends Thread
{  DataInputStream in;
   DataOutputStream out;
   Socket sock;
   Client fp;
   String name;
   boolean flag=true;
   Say(Client fp,Socket sock)
   { 
      this.fp=fp;
   	  this.sock=sock;
   	  try{
   	    in=new DataInputStream(sock.getInputStream());
        out=new DataOutputStream(sock.getOutputStream());
        this.start();
      }catch(Exception ee){}
   }
   public void run()
   {  String type=null;
      String msg=null;
   	  String name=null;
   	  String pwd=null;
   	  while(flag)
   	  {  try{
   	       type=in.readUTF();
           if(type.equals("refresh"))
           { 
           	  fp.dlm.clear();
           	  fp.dlm.addElement("所有");
           	  fp.list.setSelectedIndex(0);
      
           }
           else if(type.equals("name"))
           {  name=in.readUTF();
           	  fp.dlm.addElement(name);
           }
           else if(type.equals("quit"))
           {
             fp.msgShow.append("服务器已关闭\n");   	    
             flag=false;   	
           }
           else if(type.equals("msg"))
           {
           	  msg=in.readUTF();
              fp.msgShow.append(msg+"\n");	
           }
           else if(type.equals("equals"))
           {
           	 JOptionPane.showMessageDialog(null,"用户名已被使用!连接服务器失败!");
           	 break;
           }
           else if(type.equals("serverquit"))
           {
           	 JOptionPane.showMessageDialog(null,"服务器已经关闭!");
           	 System.exit(0);
             break;
           }
           
         }catch(Exception ee){break;}
     }
     try{
       sock.close();
     }catch(Exception ee){}
   }
}

⌨️ 快捷键说明

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