package client; |
import java.awt.BorderLayout; |
import java.awt.CardLayout; |
import java.awt.Color; |
import java.awt.GridLayout; |
import java.awt.event.MouseEvent; |
import java.awt.event.MouseListener; |
import java.util.ArrayList; |
import java.util.Arrays; |
import java.util.List; |
import javax.swing.ImageIcon; |
import javax.swing.JButton; |
import javax.swing.JFrame; |
import javax.swing.JLabel; |
import javax.swing.JPanel; |
import javax.swing.JScrollPane; |
import server.ChatThread; |
import server.Message; |
public class FriendList extends JFrame implements MouseListener{ |
|
JPanel jphy1,jphy2,jphy3; |
JButton jphy_jb1,jphy_jb2,jphy_jb3; |
JScrollPane jsp1; |
String owner; |
JPanel jpmsr1,jpmsr2,jpmsr3; |
JButton jpmsr_jb1,jpmsr_jb2,jpmsr_jb3; |
JScrollPane jsp2; |
public static JLabel []jbs1; |
public static JLabel []jbs2; |
CardLayout c1; |
String name; |
|
public FriendList(String name) |
{ this .name=name; |
this .setTitle(name); //设置窗体的标题 |
this .setSize( 200 , 650 ); //设置窗体的大小 |
this .setLocationRelativeTo( null ); //设置窗体出现位置居中 |
this .setDefaultCloseOperation( 3 ); //设置窗体的关闭操作 |
this .setResizable( true ); //设置禁止调整窗体的大小 |
this .setIconImage(( new ImageIcon( "src/MyPicture/qq.png" ).getImage())); //设置标题栏图标 |
//好友面板********************************************* |
jphy_jb1= new JButton( "我的好友" ); |
jphy_jb2= new JButton( "陌生人" ); |
jphy_jb2.addActionListener(e->c1.show( this .getContentPane(), "2" )); |
jphy_jb3= new JButton( "黑名单" ); |
|
jphy2= new JPanel( new GridLayout( 50 , 1 , 4 , 4 )); |
jbs1= new JLabel[ 30 ]; |
|
for ( int i= 0 ;i<jbs1.length;i++) |
{ jbs1[i]= new JLabel((i+ 1 )+ "" , new ImageIcon( "src/MyPicture/6.jpg" ),JLabel.LEFT); //给每个好友添加头像 |
jbs1[i].setEnabled( false ); //false 图标都是灰暗的,不可操作,true表示图标可操作 |
if (jbs1[i].getText().equals(name)) |
{jbs1[i].setEnabled( true );} |
jbs1[i].addMouseListener( this ); |
jphy2.add(jbs1[i]); |
} |
|
jphy3= new JPanel( new GridLayout( 2 , 1 )){{ add(jphy_jb2);add(jphy_jb3);}}; |
jsp1= new JScrollPane(jphy2); |
jphy1= new JPanel( new BorderLayout()){{add(jphy_jb1, "North" );add(jsp1, "Center" );add(jphy3, "South" ); }}; |
//好友面板********************************************* |
//黑名单********************************************* |
jpmsr_jb1= new JButton( "我的好友" ); |
jpmsr_jb1.addActionListener(e->c1.show( this .getContentPane(), "1" )); |
jpmsr_jb3= new JButton( "黑名单" ); |
|
jpmsr2= new JPanel( new GridLayout( 20 , 1 , 4 , 4 )); |
jbs2= new JLabel[ 20 ]; |
|
for ( int i= 0 ;i<jbs2.length;i++) |
{ jbs2[i]= new JLabel((i+ 21 )+ "" , new ImageIcon( "src/MyPicture/6.jpg" ),JLabel.LEFT); //给每个好友添加头像 |
jbs2[i].setEnabled( false ); //false 图标都是灰暗的,不可操作,true表示图标可操作 |
jbs2[i].addMouseListener( this ); |
jpmsr2.add(jbs2[i]); |
} |
|
jpmsr3= new JPanel( new GridLayout( 2 , 1 )){{add(jpmsr_jb1);add( new JButton( "陌生人" ));}}; //2行一列,将jpmsr_jb1,jpmsr_jb2变成一个上部面板 |
jsp2= new JScrollPane(jpmsr2); //添加中间面板 |
|
|
jpmsr1= new JPanel( new BorderLayout()){{add(jpmsr3, "North" );add(jsp2, "Center" );add(jpmsr_jb3, "South" ); }}; |
//黑名单********************************************* |
c1= new CardLayout(); |
this .setLayout(c1); //详细看下CardLayout的知识 |
this .add(jphy1, "1" ); //将面板jphy1添加到CardLayout的1号位置 |
this .add(jpmsr1, "2" ); //将面板jpmsr1添加到CardLayout的2号位置 |
this .setSize( 250 , 600 ); //设置面板大小 |
this .setVisible( true ); |
} |
|
public void mousePressed(MouseEvent e) { |
} |
public void mouseReleased(MouseEvent e) { |
} |
public void mouseEntered(MouseEvent e) |
{ ((JLabel)e.getSource()).setForeground(Color.red); //鼠标移动到头像上,头像变红 |
} |
public void mouseExited(MouseEvent e) |
{ ((JLabel)e.getSource()).setForeground(Color.BLACK); //鼠标移动到头像上,头像变黑 |
} |
public void mouseClicked(MouseEvent e) |
{ if (e.getClickCount()== 2 ) |
{ String friendNo= ((JLabel)e.getSource()).getText(); //得到聊天的好友编号 |
String sss= this .name+ " " +friendNo; |
ChatThread.addQqChat(sss, new Chat( this .name,friendNo)); // 这行代码检查错误用了1小时,错误代码是ManageQqChat.hm1.put(sss,a) |
} |
} |
|
public void updateFriend(Message m) |
{ |
//让对应好友图标变亮 |
Arrays.asList(m.getCon().split( " " )).forEach(i->jbs1[Integer.parseInt(i)- 1 ].setEnabled( true )) ; |
} |
} |
package s122803; |
import java.awt.BorderLayout; |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionListener; |
import java.io.*; |
import java.net.*; |
import javax.swing.JButton; |
import javax.swing.JFrame; |
import javax.swing.JPanel; |
import javax.swing.JScrollPane; |
import javax.swing.JTextArea; |
import javax.swing.JTextField; |
public class Server extends JFrame implements ActionListener { |
JTextArea jta = null ; |
JButton jb = null ; |
JTextField jtf = null ; |
JPanel jp = null ; |
JScrollPane jsp = null ; |
PrintWriter pw = null ; |
public Server() |
{ |
jta = new JTextArea(); |
jb = new JButton( "发送" ); |
jb.addActionListener( this ); // |
jtf = new JTextField( 15 ); |
jp = new JPanel(); |
jp.add(jtf); |
jp.add(jb); |
jsp = new JScrollPane(jta); |
this .setTitle( "服务器" ); // 设置窗体的标题 |
this .setSize( 400 , 300 ); // 设置窗体的大小 |
this .setDefaultCloseOperation( 3 ); // 设置关闭的方式 |
this .setLocationRelativeTo( null ); // 设置窗体出现在屏幕中间 |
this .setResizable( false ); // 设置窗体不可改变大小 |
this .setLayout( new BorderLayout()); // 设置窗体为空布局 |
this .add(jsp, BorderLayout.CENTER); |
this .add(jp, BorderLayout.SOUTH); |
this .setVisible( true ); |
// 设置窗体属性************************************************************ |
try { |
ServerSocket ss = new ServerSocket( 9999 ); // 设置服务器端口是9999 |
Socket s = ss.accept(); // 监听是否有客户端登陆,只有为true才会执行下面的代码 |
// 而且监听到的端口赋给s,s就表示连接的客户端端口 |
BufferedReader br = new BufferedReader( new InputStreamReader(s.getInputStream())); |
/* |
* s是客户端端口,s.getInputStream这个方法是获取客户端的输入流,从而获取客户端发送的数据 |
* |
* 上面这一句可以写成2句话 InputStreamReader aa=new |
* InputStreamReader(s.getInputStream()); |
* |
* BufferedReader br=new BufferedReader(aa); 将字节流放入缓冲池 |
*/ |
pw = new PrintWriter(s.getOutputStream(), true ); |
jta.setText( "客户端已接入" + "\r\n" ); |
String aa = "已成功连接服务器" ; |
pw.println(aa); |
while ( true ) |
{ |
String xinxi = br.readLine(); |
jta.append( "客户端:" + xinxi + "\r\n" ); |
} |
} catch (Exception e) { |
} |
} |
public static void main(String[] args) { |
new Server(); |
} |
public void actionPerformed(ActionEvent e) { |
if (e.getActionCommand().equals( "发送" )) { |
String xinxi = jtf.getText(); |
jta.append( "服务器:" + xinxi + "\r\n" ); |
pw.println(xinxi); |
jtf.setText( "" ); |
} |
} |
} |