import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import sun.awt.WindowClosingListener;
public class IMPanelClient extends JFrame {
/**
* Launch the application
* @param args
*/
static Socket clientSocket;
static ObjectOutputStream dataOut;
static ObjectInputStream dataIn;
static String userName;
ClientReceiveThread rt;
final JComboBox onlineBox = new JComboBox();
public static void main(String args[]) {
try {
IMPanelClient frame = new IMPanelClient(clientSocket, dataIn, dataOut, userName);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
* @param userName
* @param dataOut
* @param dataIn
* @param clientSocket
*/
public IMPanelClient(final Socket clietSocket,ObjectInputStream dataIn,final ObjectOutputStream dataOut,final String userName) {
super();
this.clientSocket=clietSocket;
this.dataIn=dataIn;
this.dataOut=dataOut;
this.userName=userName;
setTitle(userName+"的聊天窗口");
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel_1 = new JPanel();
panel_1.setLayout(new BorderLayout());
getContentPane().add(panel_1);
final JTextField textField = new JTextField();
textField.setPreferredSize(new Dimension(0, 100));
panel_1.add(textField, BorderLayout.SOUTH);
final JScrollPane scrollPane = new JScrollPane();
panel_1.add(scrollPane);
final JTextArea receiveMeg = new JTextArea();
scrollPane.setViewportView(receiveMeg);
rt=new ClientReceiveThread(clietSocket,dataOut,dataIn,receiveMeg,onlineBox);
final JPanel panel_2 = new JPanel();
final FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(FlowLayout.RIGHT);
panel_2.setLayout(flowLayout);
getContentPane().add(panel_2, BorderLayout.SOUTH);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(clietSocket!=null&&!clietSocket.isClosed()){
try{
String message=textField.getText();
String toSomebody=(String)onlineBox.getSelectedItem();
dataOut.writeObject("聊天信息");
dataOut.flush();
dataOut.writeObject(toSomebody);
dataOut.flush();
dataOut.writeObject(message);
dataOut.flush();
Date d=new Date();
SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr=s.format(d);
receiveMeg.append(userName+" "+dateStr+"\n"+message+"\n");
textField.setText("");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
button.setText("发送");
panel_2.add(button);
onlineBox.setModel(new DefaultComboBoxModel(new String[] {"所有人"}));
panel_2.add(onlineBox);
rt=new ClientReceiveThread(clietSocket,dataOut,dataIn,receiveMeg,onlineBox);
rt.start();
}
public void windowClosing(WindowEvent e){
if(clientSocket!=null&&!clientSocket.isClosed()){
try{
dataOut.writeObject("用户下线");
dataOut.flush();
Thread.sleep(1000);
clientSocket.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
socket聊天室可以私人对聊,服务器可以观察聊天记录
需积分: 49 174 浏览量
2017-12-27
16:02:25
上传
评论
收藏 26KB ZIP 举报
「已注销」
- 粉丝: 1
- 资源: 2
最新资源
- 自学文档,用于自己学习
- asp代码ASP考试成绩分析系统的设计与实现(源代码+论文)
- openssh-9.7p1-1.ky10.aarch64-no-ssh-copy-id.tgz
- asp代码ASP具有动态口令认证机制的网上投票系统的设计(源代码+论文)
- asp代码ASP局域网文件共享及检索系统的设计与开发(源代码+论文)
- asp代码ASP酒店预定管理系统设计(论文+源代码+开题报告)
- javaweb毕业设计-学生住宿管理系统JAVA(可做课程设计).rar
- asp代码ASP教师信息管理系统设计(源代码+论文)
- c语言链表的基本操作实现
- python冒泡排序(Bubble Sort).docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈