package client;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
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.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import entity.Message;
/**
* 即时通讯系统客户端
*
* @author Teacher mfx
*
*/
public class Client extends JFrame implements ActionListener {
// 菜单
JMenuBar menuBar = new JMenuBar();
JMenu adminMenu = new JMenu("连接");
JMenu aboutMenu = new JMenu("关于");
JMenuItem a1 = new JMenuItem("连接服务器");
JMenuItem a2 = new JMenuItem("断开服务器");
JMenuItem a3 = new JMenuItem("退出");
JMenuItem a4 = new JMenuItem("关于聊吧");
JPanel north = new JPanel();
ImageIcon top_icon = new ImageIcon(Client.class.getResource("top.jpg"));
JLabel top_label = new JLabel(top_icon);
// ImageIcon icon =
// new ImageIcon(
// Client.class.getResource("top.jpg"));
//
// 左边
JPanel west = new JPanel();
JPanel west_top = new JPanel();
// JPanel west_center = new JPanel();
ImageIcon west_icon = new ImageIcon(Client.class
.getResource("leftPhoto.jpg"));
JLabel west_label = new JLabel(west_icon);
DefaultListModel dlm = new DefaultListModel();
// String[] namesss = {"gergg","hyh"};
JList userList = new JList(dlm);
JScrollPane userPane = new JScrollPane(userList);
// 中部
JPanel center = new JPanel();
JTextArea msg_area = new JTextArea(12, 25);
JScrollPane show_msg = new JScrollPane(msg_area);
JPanel userOper = new JPanel();
JLabel input_label = new JLabel("请输入:");
JTextField input_field = new JTextField(40);
JButton send = new JButton("发送");
// 网络
Socket s;
ObjectInputStream ois;
ObjectOutputStream oos;
String name;
public Client() {
setTitle("安康聊天吧");
Image titleImage = Toolkit.getDefaultToolkit().createImage(
"./src/Client/logo.jpg");
this.setIconImage(titleImage);
this.setLocation(100, 100);
this.setSize(800, 600);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.NORMAL);
init();
addEvent();
this.setVisible(true);
}
public void init() {
msg_area.setFont(new Font("仿宋", Font.BOLD, 25));
// 设置颜色
menuBar.setBackground(Color.CYAN);
north.setBackground(Color.BLUE);
west_top.setBackground(Color.yellow);
userList.setBackground(Color.cyan);
msg_area.setBackground(Color.lightGray);
userOper.setBackground(Color.green);
// 上部设置
north.setLayout(new BorderLayout());
north.setSize(800, 50);
adminMenu.add(a1);
adminMenu.add(a2);
adminMenu.add(a3);
aboutMenu.add(a4);
menuBar.add(adminMenu);
menuBar.add(aboutMenu);
north.add(top_label, BorderLayout.CENTER);
north.add(menuBar, BorderLayout.NORTH);
// 左部设置
west.setSize(130, 550);
west_top.setSize(130, 150);
userPane.setSize(130, 400);
west_top.add(west_label);
west.setLayout(new BorderLayout());
west.add(west_top, BorderLayout.NORTH);
west.add(userPane, BorderLayout.CENTER);
// 中部设置
center.setSize(670, 400);
show_msg.setSize(670, 340);
userOper.setSize(670, 60);
msg_area.setEditable(false);
input_field.setActionCommand("enterKey");
userOper.setLayout(new FlowLayout(FlowLayout.LEFT));
userOper.add(input_label);
userOper.add(input_field);
userOper.add(send);
center.setLayout(new BorderLayout());
center.add(show_msg, BorderLayout.NORTH);
center.add(userOper, BorderLayout.CENTER);
add(north, BorderLayout.NORTH);
add(west, BorderLayout.WEST);
add(center, BorderLayout.CENTER);
}
public void addEvent() {
a1.addActionListener(this);
a2.addActionListener(this);
a3.addActionListener(this);
a4.addActionListener(this);
send.addActionListener(this);
input_field.addActionListener(this);
WindowAdapter adapter = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int temp = JOptionPane.showConfirmDialog(new JFrame(),
"确认要退出吗?", "确认退出", JOptionPane.YES_NO_OPTION);
if (temp == 0) {
try {
Message mg = new Message();
mg.setForm(name);
mg.setMsgType("quit");
oos.writeObject(mg);
oos.close();
ois.close();
s.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
// e1.printStackTrace();
}
System.exit(0);
}
}
};
this.addWindowListener(adapter);
}
@Override
public void actionPerformed(ActionEvent e){
String comm = e.getActionCommand();
System.out.println(comm);
if ("连接服务器".equals(comm)) {
if(s != null){
JOptionPane.showMessageDialog(this, "已登录");
return;
}
int cnt = 1;
while(true){
if(cnt >= 4){
System.exit(0);
}
String ip = JOptionPane.showInputDialog(getContentPane(),"输入服务器地址");
String port = JOptionPane.showInputDialog(getContentPane(),"输入服务器端口号");
try {
s = new Socket(ip, Integer.parseInt(port));
if(s != null){
System.out.println("client"+s.getOutputStream().hashCode());
break;
}
} catch (Exception e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(getContentPane(), "服务器信息有误!!!");
}
cnt++;
}
name = JOptionPane.showInputDialog(getContentPane(),"输入用户名");
TitleThread tt = new TitleThread(this);
tt.start();
ReadThread rt = new ReadThread();
rt.start();
Message newUser = new Message();
newUser.setForm(name);
newUser.setMsgType("login");
try {
oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(newUser);
oos.flush();
} catch (Exception e1) {
e1.printStackTrace();
}
}else if("断开服务器".equals(comm)){
}else if("退出".equals(comm)){
int temp = JOptionPane.showConfirmDialog(this.getContentPane(),"确认要退出吗?","确认退出",JOptionPane.YES_NO_OPTION );
if(temp == 0){
System.exit(0);
}
}else if("关于聊吧".equals(comm)){
JFrame jf = new JFrame("关于聊吧");
JLabel jl = new JLabel(new ImageIcon("./src/client/hjs.gif"));
String str = "【即时通讯系统】\n 版本:v1.0 \n 技术总监:孟凡新 \n 软件设计:孟凡新 \n 编码人员:孟凡新 \n 日期:2010年4月16日 \n E-mail:mengfanxin.1987@yahoo.com.cn \n 公司:北京达内科技有限公司";
JTextPane jtp = new JTextPane();
jtp.setText(str);
jtp.setEditable(false);
Color c = new Color(187,122,233,58);
jtp.setBackground(c);
jf.setSize(300,200);
jf.setLocation(300,200);
jf.add(jl,BorderLayout.WEST);
jf.add(jtp,BorderLayout.CENTER);
jf.setVisible(true);
}else if("发送".equals(comm) || "enterKey".equals(comm)){
String content = input_field.getText();
System.out.println("content:"+content);
System.out.println(content != null && content.trim().equals(""));
if(content != null && !content.trim().equals("")){
System.out.println("content1:"+content);
Message mess = new Message();
mess.setForm(name);
mess.setMsg(content);
mess.setMsgType("chat");
try {
oos.writeObject(mess);
oos.flush();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
class TitleThread extends Thread{