import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.*;
import java.security.Key;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import Encrypt.Encrypt;
public class ChatClient extends JFrame {
//消息类型
public static int USER_CHAT = 1;//用户聊天信息标识
public static int USER_LOGIN = 2;//用户登陆信息标识
public static int USER_OFFLINE = 3;//用户下线信息标识
public static int USER_WHISPER = 4;
public static JCheckBox whisperCheckBox = new JCheckBox();
public static JCheckBox decryptionCheckBox = new JCheckBox();
public static JLabel label = new JLabel();//如果没选ComBox就私聊,提示信息的标签
public static JLabel labelCount = new JLabel();//显示在线人数的标签
public static JList list;
public static JTextArea textArea;
public static JTextField textField;
public static JTextField keyField;
public static Image imageIcon;
public static ObjectOutputStream oos = null;
public static ObjectInputStream ois = null;
public static Socket s = null;
public static String nickName;
public static DefaultListModel model;
public static String pre = "";
/*
* send方法用来发送客户昵称
*/
public void send() {
Message m = new Message(nickName,null, USER_LOGIN,null);
try {
oos.writeObject(m);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void receive() {//接受线程
new Thread(new ReceiveThread()).start();
}
public ChatClient(final String nickName) {
super();
getContentPane().setForeground(new Color(128, 0, 0));
setResizable(false);
imageIcon =Toolkit.getDefaultToolkit().getImage("laugh1.jpg");//设置标题图片
setIconImage(imageIcon);
getContentPane().setLayout(null);
setTitle(nickName+":欢迎来到聊天室!");
setBounds(100, 100, 566, 570);
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.nickName = nickName;
textField = new JTextField();
textField.setBounds(0, 500, 419, 35);
textField.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
sendMsg();
}
});
final JLabel keyLabel = new JLabel();
keyLabel.setForeground(new Color(128, 128, 0));
keyLabel.setText("输入密钥:");
keyLabel.setBounds(0, 447, 60, 25);
getContentPane().add(keyLabel);
keyField = new JTextField();
keyField.setText("123456789");
keyField.setBounds(60, 447, 210, 25);
getContentPane().add(keyField);
addWindowListener(new WindowAdapter() {//用户关闭窗口退出,给服务器发退出消息
public void windowClosing(WindowEvent e) {
Message m = new Message(nickName,null, USER_OFFLINE, null);
try {
oos.writeObject(m);
oos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
getContentPane().add(textField);
connect();
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 0, 419, 441);
getContentPane().add(scrollPane);
textArea = new JTextArea();
textArea.setForeground(new Color(0, 0, 255));
textArea.setFont(new Font("", Font.BOLD, 14));
//textArea.setBackground(new Color(245, 222, 179));
textArea.setLineWrap(true);
scrollPane.setViewportView(textArea);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
final JButton clearTextButton = new JButton();
final JButton sendTextButton = new JButton();
clearTextButton.addActionListener(new ActionListener() {//清除按钮动作
public void actionPerformed(final ActionEvent arg0) {
textArea.setText("");
}
});
clearTextButton.setText("清空消息框");
clearTextButton.setBounds(425, 415, 125, 22);
getContentPane().add(clearTextButton);
list = new JList();
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(final ListSelectionEvent arg0) {
label.setText("");
label.setVisible(true);
}
});
list.setBackground(Color.LIGHT_GRAY);
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent arg0) {
pre = (String)list.getSelectedValue();
}
});
list.setBounds(425, 61, 125, 325);
getContentPane().add(list);
final JLabel friendsListLabel = new JLabel();
friendsListLabel.setForeground(new Color(128, 0, 0));
friendsListLabel.setFont(new Font("", Font.BOLD, 14));
friendsListLabel.setText("在线用户");
friendsListLabel.setBounds(447, 10, 83, 22);
getContentPane().add(friendsListLabel);
final JLabel pressEnterToLabel = new JLabel();
pressEnterToLabel.setForeground(new Color(128, 128, 0));
pressEnterToLabel.setText("you can Press Enter to send");
pressEnterToLabel.setBounds(0, 480, 210, 18);
getContentPane().add(pressEnterToLabel);
whisperCheckBox.setText("与他私聊");
whisperCheckBox.setBounds(425, 390, 125, 21);
getContentPane().add(whisperCheckBox);
decryptionCheckBox.setText("显示明文");
decryptionCheckBox.setBounds(330, 447, 125, 21);
getContentPane().add(decryptionCheckBox);
sendTextButton.setText("发送");
sendTextButton.setBounds(425, 500, 90, 35);
getContentPane().add(sendTextButton);
sendTextButton.addActionListener(new ActionListener() {//发送消息按钮动作
public void actionPerformed(final ActionEvent arg0) {
sendMsg();
}
});
label.setBounds(180, 480, 261, 18);
label.setForeground(new Color(255, 0, 0));
getContentPane().add(label);
final JLabel label_1 = new JLabel();
label_1.setForeground(new Color(128, 0, 0));
label_1.setText("(");
label_1.setBounds(498, 38, 4, 18);
getContentPane().add(label_1);
final JLabel label_1_1 = new JLabel();
label_1_1.setForeground(new Color(128, 0, 0));
label_1_1.setText(")");
label_1_1.setBounds(526, 38, 4, 18);
getContentPane().add(label_1_1);
labelCount.setBounds(508, 38, 12, 18);
labelCount.setForeground(new Color(128, 0, 0));
getContentPane().add(labelCount);
final JLabel countLabel = new JLabel();
countLabel.setForeground(new Color(128, 0, 0));
countLabel.setText("用户数:");
countLabel.setBounds(426, 38, 66, 18);
getContentPane().add(countLabel);
model=new DefaultListModel();
model.addElement("Everyone");
}
/*
* 连接服务端
*/
public void connect() {
try {
s = new Socket("127.0.0.1", 6666);
oos = new ObjectOutputStream(s.getOutputStream());
ois = new ObjectInputStream(s.getInputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void sendMsg(){
int flag = 1;//用于标示pre(选中的用户昵称)是否为Everyone或者他自己或者空字符串,如果是Everyone,则不予以发送
String s;
Encrypt encrypt=new Encrypt();
String key=keyField.getText();
if(!whisperCheckBox.isSelected()) {//如果公聊,发送USER_CHAT信息
label.setText("");
try {
Message m = null ;
if(!pre.equals("")) {//如果在列表中选了用户
if(pre.equals(nickName)) {//如果在列表中选的用户是自己,则不予以发送
flag = 0;
label.setText("不能自己跟自己聊天!");
label.setVisible(true);
}
Stri