package client; |
import java.awt.BorderLayout; |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionListener; |
import java.io.ObjectOutputStream; |
import java.io.PrintWriter; |
import java.util.Date; |
import javax.swing.JButton; |
import javax.swing.JFrame; |
import javax.swing.JPanel; |
import javax.swing.JScrollPane; |
import javax.swing.JTextArea; |
import javax.swing.JTextField; |
import server.ManagerClientConServerThread; |
import server.Message; |
//因为客户端要不断读取信息,所以也是一个线程类 |
public class Chat extends JFrame implements ActionListener |
{ static String receiver; |
//声明各组件************************ |
JTextArea jta= null ; |
JButton jb= null ; |
JTextField jtf= null ; |
JPanel jp= null ; |
JScrollPane jsp= null ; |
PrintWriter pw= null ; |
//声明各组件************************ |
|
String ownername; |
String friendname; |
public Chat(String ownername,String friendname) |
{ this .ownername=ownername; |
this .friendname=friendname; |
//设置窗体属性************************************************************ |
jta= new JTextArea(); //创建文本域对象 |
jta.setEditable( false ); |
jb= new JButton( "发送" ); //创建按钮对象 |
jb.addActionListener( this ); //给按钮添加事件源对象 |
jtf= new JTextField( 15 ); //创建文本框对象 |
jp= new JPanel(){{ add(jtf);add(jb); }}; //创建界面对象jp,在界面中添加文本框和按钮 |
jsp= new JScrollPane(jta); //在滚动条中添加文本域 |
|
this .setTitle(ownername+ "正在和" +friendname+ "聊天" ); //设置窗体的标题 |
this .setSize( 400 , 300 ); //设置窗体的大小 |
this .setDefaultCloseOperation( 2 ); //关闭一个聊天框,不是全部关闭,所以选择方式2,方式3是关闭全部窗体 |
this .setLocationRelativeTo( null ); //设置窗体出现在屏幕中间 |
this .setResizable( false ); //设置窗体不可改变大小 |
this .setLayout( new BorderLayout()); //设置窗体为空布局 |
this .add(jsp,BorderLayout.CENTER); //将面板jsp添加到窗体的中间区域 |
this .add(jp,BorderLayout.SOUTH); //将面板jp添加到窗体的下方区域 |
this .setVisible( true ); //设置窗体可见 |
//设置窗体属性************************************************************ |
} |
public void actionPerformed(ActionEvent e) //发送信息 |
{ |
Message m= new Message(); |
m.setSender( this .ownername); //要发什么就封装什么 |
m.setGetter( this .friendname); |
m.setCon(jtf.getText()); |
m.setSendtime( new Date().toString()); |
m.setMesType( "3" ); |
|
try |
{ |
String xinxi=jtf.getText(); |
String time= new Date().toString(); |
jta.append(m.getSender()+ ":" +xinxi+ "\t" +time+ "\r\n" ); |
//得到自己的线程 |
ObjectOutputStream oos= new ObjectOutputStream(ManagerClientConServerThread.getClientConServerThread(m.getSender()).getS().getOutputStream()); |
oos.writeObject(m); |
jtf.setText( "" ); |
|
} catch (Exception e2){e2.printStackTrace();} |
} |
|
public void setText(Message m) |
{ |
jta.append(m.getSender()+ ":" +m.getCon()+ "\r\n" ); |
} |
} |
package client; |
import java.io.ObjectInputStream; |
import java.io.ObjectOutputStream; |
import java.net.InetSocketAddress; |
import java.net.Socket; |
import java.net.SocketAddress; |
import server.ManagerClientConServerThread; |
import server.Message; |
import server.User; |
public class ClientConnectServer { |
public Socket s; |
// 发送第一次请求 |
public boolean sendLoginInfoToServer(Object o) |
{ |
boolean b = false ; |
try { |
long beginTime=System.currentTimeMillis(); |
s= new Socket(); |
SocketAddress socketAddress= new InetSocketAddress( "127.0.0.1" , 9999 ); //InetSocketAddress(服务器地址,端口号) |
s.connect(socketAddress, 60000 ); //设定连接等待时间60000毫秒 |
long endTime=System.currentTimeMillis(); |
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream()); |
oos.writeObject(o); |
Message m = (Message) new ObjectInputStream(s.getInputStream()).readObject(); //读取对象流的信息 |
if (m.getMesType().equals( "1" )) { |
// 就创建一个该qq号和服务器端保持通讯连接得线程 |
ClientConServerThread ccst = new ClientConServerThread(s); |
new Thread(ccst).start(); |
ManagerClientConServerThread.addClientConServerThread( ((User) o).getName(), ccst); |
b = true ; |
long connectTime=endTime-beginTime; |
System.out.println(socketAddress+ ":" +connectTime); //输出连接花费的时间 |
} |
|
else s.close(); // 关闭Scoket |
} catch (Exception e) {e.printStackTrace();} |
return b; |
} |
public Socket getSocket() { |
return s; |
} |
public void SendInfoToServer(Object o) { |
|
} |
} |
package client; |
//客户端和服务器保持通讯的类 |
import java.io.*; |
import java.net.*; |
import server.ChatThread; |
import server.FriendListThread; |
import server.Message; |
public class ClientConServerThread implements Runnable { |
private Socket s; |
public Socket getS() { |
return s; |
} |
public void setS(Socket s) { |
this .s = s; |
} |
public ClientConServerThread(Socket s) { |
this .s = s; |
} |
public void run() { |
while ( true ) |
{ |
try { |
Message m = (Message) new ObjectInputStream(s.getInputStream()).readObject(); //读取对象流数据存入m中 |
|
if (m.getMesType().equals( "3" )) //如果是普通信息包 |
{ |
ChatThread.get(m.getGetter() + " " + m.getSender()).setText(m); |
} |
|
else if (m.getMesType().equals( "4" )) //如果是好友在线包 |
{ |
FriendList friendlist=FriendListThread.getQqFriendList(m.getGetter()); |
friendlist.updateFriend(m); //更新在线的朋友 |
} |
|
|
} catch (Exception e) {} |
} |
} |
} |
package client; |
import server.User; |
public class ClientUser { |
public boolean checkUser(User u) |
{ |
return new ClientConnectServer().sendLoginInfoToServer(u); |
} |
|
} |
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 )) ; |
} |
} |
中级程序员
by: 猴子的救兵 发表于:2017-09-13 22:52:59 顶(0) | 踩(0) 回复
怎么没人评价呢
回复评论