[Java] fdhds →→→→→进入此内容的聊天室

来自 , 2021-03-23, 写在 Java, 查看 150 次.
URL http://www.code666.cn/view/cf1f78fe
  1. package client;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.CardLayout;
  5. import java.awt.Color;
  6. import java.awt.GridLayout;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseListener;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.List;
  12.  
  13. import javax.swing.ImageIcon;
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JPanel;
  18. import javax.swing.JScrollPane;
  19.  
  20. import server.ChatThread;
  21. import server.Message;
  22.  
  23.  
  24.  
  25. public class FriendList extends JFrame implements MouseListener{
  26.        
  27.         JPanel jphy1,jphy2,jphy3;
  28.         JButton jphy_jb1,jphy_jb2,jphy_jb3;
  29.         JScrollPane jsp1;
  30.         String owner;
  31.         JPanel jpmsr1,jpmsr2,jpmsr3;
  32.         JButton jpmsr_jb1,jpmsr_jb2,jpmsr_jb3;
  33.         JScrollPane jsp2;
  34.         public static JLabel []jbs1;
  35.         public static JLabel []jbs2;
  36.         CardLayout c1;
  37.         String name;
  38.        
  39.   public  FriendList(String name)
  40.   { this.name=name;
  41.         this.setTitle(name);                                                                                                     //设置窗体的标题
  42.     this.setSize(200, 650);                                                                                              //设置窗体的大小
  43.     this.setLocationRelativeTo(null);                                                                                    //设置窗体出现位置居中
  44.     this.setDefaultCloseOperation(3);                                                                                    //设置窗体的关闭操作
  45.     this.setResizable(true);                                                     //设置禁止调整窗体的大小
  46.     this.setIconImage((new ImageIcon("src/MyPicture/qq.png").getImage()));//设置标题栏图标
  47.     //好友面板*********************************************
  48.     jphy_jb1=new JButton("我的好友");
  49.     jphy_jb2=new JButton("陌生人");
  50.     jphy_jb2.addActionListener(e->c1.show(this.getContentPane(),"2"));
  51.     jphy_jb3=new JButton("黑名单");
  52.    
  53.     jphy2=new JPanel(new GridLayout(50,1,4,4));                                
  54.     jbs1=new JLabel[30];
  55.    
  56.     for(int i=0;i<jbs1.length;i++)
  57.     {  jbs1[i]=new JLabel((i+1)+"",new ImageIcon("src/MyPicture/6.jpg"),JLabel.LEFT); //给每个好友添加头像
  58.        jbs1[i].setEnabled(false); //false 图标都是灰暗的,不可操作,true表示图标可操作
  59.        if(jbs1[i].getText().equals(name))
  60.        {jbs1[i].setEnabled(true);}
  61.        jbs1[i].addMouseListener(this);
  62.        jphy2.add(jbs1[i]);
  63.     }
  64.    
  65.     jphy3=new JPanel(new GridLayout(2,1)){{ add(jphy_jb2);add(jphy_jb3);}};
  66.     jsp1=new JScrollPane(jphy2);
  67.  
  68.     jphy1=new JPanel(new BorderLayout()){{add(jphy_jb1,"North");add(jsp1,"Center");add(jphy3,"South"); }};
  69.  
  70.     //好友面板*********************************************
  71.     //黑名单*********************************************
  72.     jpmsr_jb1=new JButton("我的好友");
  73.     jpmsr_jb1.addActionListener(e->c1.show(this.getContentPane(),"1"));
  74.     jpmsr_jb3=new JButton("黑名单");
  75.    
  76.     jpmsr2=new JPanel(new GridLayout(20,1,4,4));  
  77.      jbs2=new JLabel[20];
  78.    
  79.     for(int i=0;i<jbs2.length;i++)
  80.     {  jbs2[i]=new JLabel((i+21)+"",new ImageIcon("src/MyPicture/6.jpg"),JLabel.LEFT); //给每个好友添加头像
  81.        jbs2[i].setEnabled(false); //false 图标都是灰暗的,不可操作,true表示图标可操作
  82.        jbs2[i].addMouseListener(this);
  83.        jpmsr2.add(jbs2[i]);
  84.     }
  85.    
  86.     jpmsr3=new JPanel(new GridLayout(2,1)){{add(jpmsr_jb1);add(new JButton("陌生人"));}}; //2行一列,将jpmsr_jb1,jpmsr_jb2变成一个上部面板
  87.     jsp2=new JScrollPane(jpmsr2);   //添加中间面板
  88.    
  89.    
  90.     jpmsr1=new JPanel(new BorderLayout()){{add(jpmsr3,"North");add(jsp2,"Center");add(jpmsr_jb3,"South"); }};
  91.  
  92.     //黑名单*********************************************
  93.      c1=new CardLayout();
  94.     this.setLayout(c1);  //详细看下CardLayout的知识
  95.     this.add(jphy1,"1");   //将面板jphy1添加到CardLayout的1号位置
  96.     this.add(jpmsr1,"2");  //将面板jpmsr1添加到CardLayout的2号位置
  97.     this.setSize(250, 600);//设置面板大小
  98.     this.setVisible(true);
  99.   }
  100.  
  101.  
  102.        
  103.         public void mousePressed(MouseEvent e) {
  104.         }
  105.  
  106.         public void mouseReleased(MouseEvent e) {
  107.         }
  108.  
  109.         public void mouseEntered(MouseEvent e)
  110.         {   ((JLabel)e.getSource()).setForeground(Color.red);//鼠标移动到头像上,头像变红
  111.         }
  112.  
  113.         public void mouseExited(MouseEvent e)
  114.         { ((JLabel)e.getSource()).setForeground(Color.BLACK);//鼠标移动到头像上,头像变黑
  115.         }
  116.  
  117.         public void mouseClicked(MouseEvent e)
  118.         {  if(e.getClickCount()==2)
  119.             {   String friendNo= ((JLabel)e.getSource()).getText();  //得到聊天的好友编号
  120.             String sss=this.name+" "+friendNo;
  121.             ChatThread.addQqChat(sss, new Chat(this.name,friendNo)); // 这行代码检查错误用了1小时,错误代码是ManageQqChat.hm1.put(sss,a)      
  122.             }
  123.         }
  124.        
  125.         public void updateFriend(Message m)
  126.         {
  127.        //让对应好友图标变亮
  128.                 Arrays.asList(m.getCon().split(" ")).forEach(i->jbs1[Integer.parseInt(i)-1].setEnabled(true)) ;
  129.         }
  130. }
  131. package s122803;
  132.  
  133. import java.awt.BorderLayout;
  134. import java.awt.event.ActionEvent;
  135. import java.awt.event.ActionListener;
  136. import java.io.*;
  137. import java.net.*;
  138. import javax.swing.JButton;
  139. import javax.swing.JFrame;
  140. import javax.swing.JPanel;
  141. import javax.swing.JScrollPane;
  142. import javax.swing.JTextArea;
  143. import javax.swing.JTextField;
  144.  
  145. public class Server extends JFrame implements ActionListener {
  146.  
  147.         JTextArea jta = null;
  148.  
  149.         JButton jb = null;
  150.  
  151.         JTextField jtf = null;
  152.  
  153.         JPanel jp = null;
  154.  
  155.         JScrollPane jsp = null;
  156.  
  157.         PrintWriter pw = null;
  158.  
  159.         public Server()
  160.  
  161.         {
  162.  
  163.                 jta = new JTextArea();
  164.  
  165.                 jb = new JButton("发送");
  166.  
  167.                 jb.addActionListener(this); //
  168.  
  169.                 jtf = new JTextField(15);
  170.  
  171.                 jp = new JPanel();
  172.                 jp.add(jtf);
  173.                 jp.add(jb);
  174.  
  175.                 jsp = new JScrollPane(jta);
  176.                 this.setTitle("服务器");// 设置窗体的标题
  177.  
  178.                 this.setSize(400, 300);// 设置窗体的大小
  179.  
  180.                 this.setDefaultCloseOperation(3);// 设置关闭的方式
  181.  
  182.                 this.setLocationRelativeTo(null);// 设置窗体出现在屏幕中间
  183.  
  184.                 this.setResizable(false);// 设置窗体不可改变大小
  185.  
  186.                 this.setLayout(new BorderLayout());// 设置窗体为空布局
  187.  
  188.                 this.add(jsp, BorderLayout.CENTER);
  189.  
  190.                 this.add(jp, BorderLayout.SOUTH);
  191.  
  192.                 this.setVisible(true);
  193.  
  194.                 // 设置窗体属性************************************************************
  195.                 try {
  196.  
  197.                         ServerSocket ss = new ServerSocket(9999); // 设置服务器端口是9999
  198.  
  199.                         Socket s = ss.accept(); // 监听是否有客户端登陆,只有为true才会执行下面的代码
  200.  
  201.                         // 而且监听到的端口赋给s,s就表示连接的客户端端口
  202.  
  203.                         BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
  204.  
  205.                         /*
  206.                          * s是客户端端口,s.getInputStream这个方法是获取客户端的输入流,从而获取客户端发送的数据
  207.                          *
  208.                          * 上面这一句可以写成2句话 InputStreamReader aa=new
  209.                          * InputStreamReader(s.getInputStream());
  210.                          *
  211.                          * BufferedReader br=new BufferedReader(aa); 将字节流放入缓冲池
  212.                          */
  213.  
  214.                         pw = new PrintWriter(s.getOutputStream(), true);
  215.  
  216.                         jta.setText("客户端已接入" + "\r\n");
  217.  
  218.                         String aa = "已成功连接服务器";
  219.  
  220.                         pw.println(aa);
  221.  
  222.                         while (true)
  223.  
  224.                         {
  225.                                 String xinxi = br.readLine();
  226.                                 jta.append("客户端:" + xinxi + "\r\n");
  227.                         }
  228.                 } catch (Exception e) {
  229.                 }
  230.         }
  231.  
  232.         public static void main(String[] args) {
  233.                 new Server();
  234.         }
  235.  
  236.         public void actionPerformed(ActionEvent e) {
  237.                 if (e.getActionCommand().equals("发送")) {
  238.                         String xinxi = jtf.getText();
  239.                         jta.append("服务器:" + xinxi + "\r\n");
  240.                         pw.println(xinxi);
  241.                         jtf.setText("");
  242.  
  243.                 }
  244.  
  245.         }
  246.  
  247. }
  248.  

回复 "fdhds"

这儿你可以回复上面这条便签

captcha