/**
* 这是与好友聊天的界面
* 因为客户端,要处于读取的状态,因此我们把它做成一个线程
*/
package com.qq.client.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
import javax.swing.ImageIcon;
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 com.qq.client.model.QqClientConServer;
import com.qq.client.tools.ManageClientConServerThread;
import com.qq.common.Message;
import com.qq.common.MessageType;
public class QqChat extends JFrame implements ActionListener {
JTextArea jta;
JTextField jtf;
JButton jb;
JPanel jp;
JScrollPane jsp;
String ownerId;
String friendId;
// public static void main(String[] args) {
// new QqChat("小四");
// }
// 构造函数
public QqChat(String friendId, String ownerId) {
this.ownerId = ownerId;
this.friendId = friendId;
jta = new JTextArea();
jtf = new JTextField(15);
jb = new JButton("发送");
jb.addActionListener(this);
jp = new JPanel();
jp.add(jtf);
jp.add(jb);
jsp = new JScrollPane(jta);
this.add(jsp, "Center");
this.add(jp, "South");
this.setTitle(ownerId + " 正在与 " + friendId + " 聊天");
this.setSize(350, 300);
this.setIconImage((new ImageIcon("image/qie.jpg")).getImage());
this.setLocationRelativeTo(null);
this.setVisible(true);
}
// 写一个方法,让它显示消息
public void showMessage(Message m){
String info=m.getSender()+" 对 "+m.getGetder()+" 说:"+m.getCon()+"\r\n";
jta.append(info);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb) {
// 如果用户点击了发送按钮
Message m = new Message();
m.setMesType(MessageType.message_comm_mes);
m.setSender(this.ownerId);
m.setGetder(this.friendId);
m.setCon(this.jtf.getText());
m.setSendTime(new Date().toString());
jta.append(ownerId + " 对 " + friendId + " 说:" + jtf.getText()
+ "\r\n");
jtf.setText("");
// 发送给服务器
try {
ObjectOutputStream oos = new ObjectOutputStream(
ManageClientConServerThread
.getClientConServerThread(ownerId).getS()
.getOutputStream());
oos.writeObject(m);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
// @Override
// public void run() {
// while(true){
// //读取[如果读不到就等待]
// try {
// ObjectInputStream ois=new
// ObjectInputStream(QqClientConServer.s.getInputStream());
// Message m=(Message)ois.readObject();
// //显示出来
// String info=m.getSender()+" 对 "+m.getGetder()+" 说:"+m.getCon()+"\r\n";
// jta.append(info);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// }
}
110
110
1/**
2* 这是与好友聊天的界面
3* 因为客户端,要处于读取的状态,因此我们把它做成一个线程
4*/
5package com.qq.client.view;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.ActionListener;
9import java.io.IOException;
10import java.io.ObjectInputStream;
11import java.io.ObjectOutputStream;
12import java.util.Date;
13import javax.swing.ImageIcon;
14import javax.swing.JButton;
15import javax.swing.JFrame;
16import javax.swing.JPanel;
17import javax.swing.JScrollPane;
18import javax.swing.JTextArea;
19import javax.swing.JTextField;
20import com.qq.client.model.QqClientConServer;
21import com.qq.client.tools.ManageClientConServerThread;
22import com.qq.common.Message;
23import com.qq.common.MessageType;
24
25public class QqChat extends JFrame implements ActionListener {
26JTextArea jta;
27JTextField jtf;
28JButton jb;
29JPanel jp;
30JScrollPane jsp;
31String ownerId;
32String friendId;
33
34// public static void main(String[] args) {
35// new QqChat("小四");
36// }
37
38// 构造函数
39public QqChat(String friendId, String ownerId) {
40this.ownerId = ownerId;
41this.friendId = friendId;
42
43jta = new JTextArea();
44jtf = new JTextField(15);
45jb = new JButton("发送");
46jb.addActionListener(this);
47jp = new JPanel();
48jp.add(jtf);
49jp.add(jb);
50jsp = new JScrollPane(jta);
51
52this.add(jsp, "Center");
53this.add(jp, "South");
54
55this.setTitle(ownerId + " 正在与 " + friendId + " 聊天");
56this.setSize(350, 300);
57this.setIconImage((new ImageIcon("image/qie.jpg")).getImage());
58this.setLocationRelativeTo(null);
59this.setVisible(true);
60}
61
62// 写一个方法,让它显示消息
63public void showMessage(Message m){
64String info=m.getSender()+" 对 "+m.getGetder()+" 说:"+m.getCon()+"\r\n";
65jta.append(info);
66}
67
68@Override
69public void actionPerformed(ActionEvent e) {
70if (e.getSource() == jb) {
71// 如果用户点击了发送按钮
72Message m = new Message();
73m.setMesType(MessageType.message_comm_mes);
74m.setSender(this.ownerId);
75m.setGetder(this.friendId);
76m.setCon(this.jtf.getText());
77m.setSendTime(new Date().toString());
78jta.append(ownerId + " 对 " + friendId + " 说:" + jtf.getText()
79+ "\r\n");
80jtf.setText("");
81// 发送给服务器
82try {
83ObjectOutputStream oos = new ObjectOutputStream(
84ManageClientConServerThread
85.getClientConServerThread(ownerId).getS()
86.getOutputStream());
87oos.writeObject(m);
88} catch (Exception e1) {
89e1.printStackTrace();
90}
91}
92}
93
94// @Override
95// public void run() {
96// while(true){
97// //读取[如果读不到就等待]
98// try {
99// ObjectInputStream ois=new
100// ObjectInputStream(QqClientConServer.s.getInputStream());
101// Message m=(Message)ois.readObject();
102// //显示出来
103// String info=m.getSender()+" 对 "+m.getGetder()+" 说:"+m.getCon()+"\r\n";
104// jta.append(info);
105// } catch (Exception e) {
106// e.printStackTrace();
107// }
108// }
109// }
110}