package second;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MyQQClient {
private String name;
private Socket socket;
private DataInputStream dis;
private DataOutputStream dos;
JFrame frame;
private TextArea oldInfoArea;
private TextArea newInfoArea;
private boolean runnable=true;
private Font font;
private File file;
ImageIcon icon;
JButton button[] = new JButton[4];
public Object notpad;
public MyQQClient() {
runnable = true;
frame = new JFrame("QQ客户端");
icon = new ImageIcon("195032.jpg");//在当前目录下加入名为195032.jpg的图片即可改变窗口左上角的咖啡图片,不愿意改就注释这行与下一行
frame.setIconImage(icon.getImage());
oldInfoArea = new TextArea(15, 20);
oldInfoArea.setEditable(false);
newInfoArea = new TextArea(8,20);
font = new Font("宋体", Font.CENTER_BASELINE, 13);
oldInfoArea.setFont(font);
newInfoArea.setFont(font);
MyQQKeyListener keylistener =new MyQQKeyListener(this);
newInfoArea.addKeyListener(keylistener);
MyQQClientListener listener = new MyQQClientListener(this);
String buttonName[] = { "表情", "离线", "聊天记录", "发送" };
Panel panel = new Panel();
panel.setLayout(new GridLayout(1, 4));
for (int i = 0; i < buttonName.length; i++) {
button[i] = new JButton(buttonName[i]);
button[i].setFont(font);
button[i].addActionListener(listener);
panel.add(button[i]);
}
frame.add(this.oldInfoArea, "North");
frame.add(this.newInfoArea, "Center");
frame.add(panel, "South");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int option = JOptionPane.showConfirmDialog(null, "是否完全退出此次聊天?",
"系统提示", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (option == JOptionPane.YES_OPTION)
// System.exit(0);
MyQQClient.this.shutDown();
}
});
frame.setSize(500, 400);
frame.setLocation(300, 200);
frame.setResizable(false);
}
public static void main(String[] args) {
MyQQClient QQClient = new MyQQClient();
QQClient.inputName();
QQClient.connect();
QQClient.createThread();
}
public void saveFile() {
String oldInfo = oldInfoArea.getText();
try {
PrintWriter pw = new PrintWriter(new FileWriter(file, true));// true,不覆盖已有文件
pw.println(oldInfo);
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 检查是否存在目标文件,不存在就新建一个
public void init() {
if (file == null) {
file = new File(name + ".employee");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void xiaXian() {
try {
dos.writeUTF("L^%#$5@#h(8yh^+_)_)(=78UTh*%@!");// 暗号, 告诉服务器,离线了
this.runnable=false;
} catch (IOException e) {
e.printStackTrace();
}
}
public void shangXian() {
try {
this.runnable=true;
this.connect();
dos.writeUTF("^&%()&(*85KJ$%@+_9)(80(8()*)(");// 暗号, 告诉服务器,重新上线
} catch (IOException e) {
e.printStackTrace();
}
}
public void shutDown() {
try {
dos.writeUTF("^%#$^$jg4653|+=--09-89jh)*");// 暗号, 告诉服务器,下线了
Thread.sleep(500);
saveFile();// 退出时将聊天记录一次性保存
this.stop();
//dis.close();
dos.close();
this.socket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(0);// 聊天窗口退出
}
public void connect() {
try {
socket = new Socket("127.0.0.1", 8088);
} catch (UnknownHostException e1) {
System.out.println("连接服务器失败!");
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
dos.writeUTF(name);// 将本用户的姓名发送给服务器
} catch (IOException e) {
e.printStackTrace();
}
}
public void createThread() {
MyQQClientReader reader = new MyQQClientReader(this);
reader.start();
}
public void inputName() {
String name = (javax.swing.JOptionPane.showInputDialog("请输入您的昵称:"))
.trim();
if (name == null || name.equals("")) {// 不接受散客聊天
JOptionPane.showMessageDialog(null, "昵称不能为空哦,请输入您的昵称!", "系统提示",
JOptionPane.ERROR_MESSAGE);
inputName();
} else {
this.setName(name);
}
init();
frame.setTitle(name);
frame.setVisible(true);
}
public void stop() {
runnable = false;
}
public boolean isRunnable() {
return runnable;
}
public DataInputStream getDis() {
return dis;
}
public DataOutputStream getDos() {
return dos;
}
public TextArea getOldTextArea() {
return oldInfoArea;
}
public TextArea getNewTextArea() {
return newInfoArea;
}
public void setName(String name) {
this.name = name;
}
}