package com.xiaoyuan.client;
import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.TitledBorder;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.Xpp3Driver;
import com.xiaoyuan.model.MessageBean;
import com.xiaoyuan.model.UserBean;
import com.xiaoyuan.server.FileUtils;
public class ChatRoom extends JFrame {
private static final long serialVersionUID = 1L;
private static JFrame frame;
private JPanel contentPane;
private JTextArea textArea;
private JTextField textField;
private JButton btnNewButton;
private JButton button_2;
private JButton button_1;
private JList list;
private DefaultListModel listModel;
private boolean flag = true;
private MessageBean messageBean;
private UserBean userBean;
private Socket socket;
private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream;
private MessageThread messageThread;// 负责接收消息的线程
private Map<String, UserBean> onLineUsers = new HashMap<String, UserBean>();// 所有在线用户
private JLabel label;
private JButton off_btn;
/**
* Create the frame.
*/
public ChatRoom(UserBean userBean) {
this.userBean = userBean;
setResizable(false);
this.setIconImage(Toolkit.getDefaultToolkit().getImage(ChatRoom.class.getResource("/images/fun01.png")));
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setTitle("用户:" + userBean.getUserName());
setBounds(100, 100, 524, 439);
contentPane = new JPanel();
contentPane.setOpaque(false);
contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(153, 153, 153), null, null, null));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(null, "\u804A\u5929\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(65, 105, 225)));
panel.setBounds(0, 0, 366, 391);
contentPane.add(panel);
panel.setLayout(null);
textField = new JTextField();
textField.setBounds(0, 344, 255, 37);
panel.add(textField);
textField.setColumns(10);
// 写消息的文本框中按回车键时事件
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
sendAll();
}
});
JButton button = new JButton("\u53D1\u9001");
button.setForeground(new Color(0, 0, 0));
button.setBounds(260, 358, 93, 23);
panel.add(button);
// 单击发送按钮时事件
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendAll();
}
});
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(0, 0, 0)));
scrollPane.setBounds(0, 0, 353, 283);
panel.add(scrollPane);
textArea = new JTextArea();
textArea.setFont(new Font("微软雅黑", Font.PLAIN, 15));
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
label = new JLabel("\u804A\u5929\u4FE1\u606F");
label.setForeground(new Color(34, 139, 34));
label.setFont(new Font("黑体", Font.PLAIN, 12));
label.setHorizontalAlignment(SwingConstants.CENTER);
scrollPane.setColumnHeaderView(label);
btnNewButton = new JButton("\u5355\u4EBA\u53D1\u9001");
btnNewButton.setForeground(new Color(255, 0, 0));
btnNewButton.setBounds(260, 309, 93, 23);
panel.add(btnNewButton);
//添加抖动事件
JButton btnNewButton_1 = new JButton("\u6296\u4E00\u4E0B!");
btnNewButton_1.setForeground(new Color(0, 0, 0));
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shake();
}
});
btnNewButton_1.setBounds(10, 293, 93, 41);
panel.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("\u5F3A\u5236\u4ED6\u4E2B\u7684\u4E0B\u7EBF\uFF01");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
downOthers();
}
});
btnNewButton_2.setBounds(119, 293, 136, 41);
panel.add(btnNewButton_2);
//单人发送的事件
btnNewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendOne();
}
});
JPanel panel_1 = new JPanel();
panel_1.setOpaque(false);
panel_1.setBounds(371, 0, 127, 391);
contentPane.add(panel_1);
panel_1.setLayout(null);
listModel = new DefaultListModel<>();
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u597D\u53CB\u5217\u8868",
TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 200, 0)));
list.setBounds(0, 10, 125, 207);
panel_1.add(list);
button_1 = new JButton("\u9000\u51FA");
//关闭按钮事件
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
closeConnection();
}
});
button_1.setBounds(10, 358, 93, 23);
panel_1.add(button_1);
JLabel lblNewLabel_1 = new JLabel("");
lblNewLabel_1.setIcon(new ImageIcon(ChatRoom.class.getResource("/images/111.gif")));
lblNewLabel_1.setBounds(0, 221, 117, 125);
panel_1.add(lblNewLabel_1);
button_2 = new JButton("");
button_2.setBounds(51, 300, 3, 4);
panel_1.add(button_2);
button_2.setOpaque(false);
off_btn = new JButton("");
off_btn.setBounds(24, 269, 3, 4);
panel_1.add(off_btn);
//抖动事件
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x = ChatRoom.this.getX();
int y = ChatRoom.this.getY();
for (int i = 0; i < 20; i++) {
if ((i & 1) == 0) {
x += 3;
y += 3;
} else {
x -= 3;
y -= 3;
}
ChatRoom.this.setLocation(x, y);
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
});
//关闭窗口的事件
addWindowListener(new WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
button_1.doClick();
}
});
connectServer(userBean);
}
//让其它用户下线
protected void downOthers() {
textArea.append("-----------------------线----------------------\r\n");
messageBean = new MessageBean();
messageBean.setType("OFF");
messageBean.setFromWhere(userBean.getUserName());
sendMessage(messageBean);
}
//抖动功能
protected void shake() {
messageBean = new MessageBean();
messageBean.setFromWhere(userBean.getUserName());
messageBean.setType("SHAKE");
sendMessage(messageBean);
}
// 连接服务器
public boolean connectServer(UserBean userBean) {
try {
socket = new Socket("localhost", 7777);// 根据本机ip和端口号建立连接
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
messageBean = new MessageBean();
messageBean.setUserBean(us