import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.applet.*;
public class client extends JFrame implements ActionListener
{
public static void main(String[] args)
{
client client_frame = new client();
}
JButton command1; //连接服务器
JButton command2;//用户登录
JButton command3;//发送信息
JButton command4;//查找在线用户
JButton command5;//添加好友
JButton command6;//注册新用户
JButton command7;//删除好友
JComboBox combobox; //好友选择
JTextArea tal; //正文部分
JTextField f1; //ip地址
JTextField f2;//服务器端口
JTextField f3;//用户名
JTextField f4;//发送的信息
JTextField f5;//好友名字
JPasswordField password;
JLabel label1;//连接服务器标识
JLabel label2;//用户登录标志
JLabel la1 ;//图标
JLabel time_lab;//时间
String IP = new String();
int port;
Socket socket;
clientthread client; // @jve:decl-index=0:
label t;
PrintWriter out1;
BufferedReader in1;
AudioClip connect ; //连接服务器音乐
AudioClip fail ; //连接失败音乐
AudioClip start ; //用户登录音乐
AudioClip leave ; //用户离开音乐
AudioClip msg ; //接收信息音乐
public client()
{
Container c = getContentPane();
Container upc = new Container();
c.setLayout(new BorderLayout(5,5));
c.add(upc,BorderLayout.NORTH); //界面上部布局
upc.setLayout(new GridLayout(2,6,15,5));
upc.add(new JLabel("服务器IP"));
f1 = new JTextField("127.0.0.1");
upc.add(f1);
JLabel temp1 = new JLabel("端口");
upc.add(temp1);
temp1.setBounds(0,0,25,12);
f2 = new JTextField("1000");
//f2.setSize(50,10);
upc.add(f2);
command1 = new JButton("连接"); //连接服务器
command1.addActionListener(this);//事件监听
upc.add(command1);
ImageIcon icon1 = new ImageIcon("1.gif");
la1 = new JLabel(icon1);
upc.add(la1);
//upc.add(command6);
upc.add(new JLabel("用户名"));
f3 = new JTextField(8);
//f1.setSize(50,10);
upc.add(f3);
upc.add(new JLabel("密码"));
password = new JPasswordField(4);
//f2.setSize(50,10);
upc.add(password);
command2 = new JButton("登录"); //登录按钮
command2.setEnabled(false);
command2.addActionListener(this);
upc.add(command2);
label2 = new JLabel("未登录");
upc.add(label2);
Container centerc = new Container(); //界面中部布局
c.add(centerc,BorderLayout.CENTER);
//centerc.setLayout(new GridLayout(1,1,5,5));
centerc.add(new JLabel("接受的信息"));
tal = new JTextArea();
tal.setEditable(false);
tal.setFont(new Font("宋体",0,15));
tal.setWrapStyleWord(true);
tal.setLineWrap(true);
tal.setCaretPosition(0);
JScrollPane scroll = new JScrollPane(tal,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//scroll.setPreferredSize(new Dimension(400,400));
scroll.revalidate();
this.getContentPane().add(scroll);
Container westc = new Container(); //界面左部布局
c.add(westc,BorderLayout.WEST);
westc.setLayout(new GridLayout(2,2,20,20));
westc.add(new JLabel("接收的信息"));
time_lab = new JLabel();
westc.add(time_lab);
Container downc = new Container(); //界面下部布局
c.add(downc,BorderLayout.SOUTH);
downc.setLayout(new GridLayout(2,4,5,5));
combobox = new JComboBox();
combobox.addItem("对所有人");
//combobox.addActionListener(this);
downc.add(combobox);
f4 = new JTextField();
downc.add(f4);
command3 = new JButton("发送信息");
command3.setEnabled(false);
command3.addActionListener(this);
downc.add(command3);
command4 = new JButton("查找在线用户");
command4.addActionListener(this);
command4.setEnabled(false);
downc.add(command4);
f5 = new JTextField("在此键入好友名字");
downc.add(f5);
command5 = new JButton("添加用户");
command5.setEnabled(false);
command5.addActionListener(this);
downc.add(command5);
command7 = new JButton("删除好友");
command7.setEnabled(false);
command7.addActionListener(this);
downc.add(command7);
command6 = new JButton("注册新用户");
command6.setEnabled(false);
command6.addActionListener(this);
downc.add(command6);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setLocation(200,150);
setVisible(true);
//装载声音文件
URL file = getClass().getResource("connect.wav"); //连接服务器
connect = java.applet.Applet.newAudioClip(file); //
file = getClass().getResource("fail.wav"); //连接失败
fail = java.applet.Applet.newAudioClip(file); //
file = getClass().getResource("start.wav"); //用户登录成功
start = java.applet.Applet.newAudioClip(file); //
file = getClass().getResource("leave.wav"); //用户离开
leave = java.applet.Applet.newAudioClip(file); //
file = getClass().getResource("msg.wav"); //收到信息
msg = java.applet.Applet.newAudioClip(file); //
t= new label();
t.start();
}
public void actionPerformed (ActionEvent e)
{
try{
out1 = new PrintWriter(socket.getOutputStream(),true);
in1 = new BufferedReader (new InputStreamReader(socket.getInputStream())); //接收服务器回复信息
//String mess = in1.readLine();//读取字符串
//if(mess!=null&&mess.indexOf("服务器已停止对你的服务")!=-1);
}
catch(Exception f){}
if(e.getSource()== command1) ////////////////连接服务器
{
connect.play(); //播放连接音乐
socket = new Socket();
try{
IP = f1.getText();
port = Integer.parseInt(f2.getText());
socket.connect(new InetSocketAddress(IP,port),5000);
Thread.sleep(1000);
if(socket.isConnected() == true) //如果连接上
{
command1.setText("已连上");
JOptionPane.showMessageDialog(null,"连接服务器成功");
command1.setEnabled(false);
f1.setEditable(false);
f2.setEditable(false);
command2.setEnabled(true);
command6.setEnabled(true);
}
}//try ends
catch(Exception f)
{
fail.play();
JOptionPane.showMessageDialog(null,"连接服务器失败");
}
}
if(e.getSource() == command2) //////////////////////// 用户登录或离开
{
try{
if((command2.getText()).compareTo("登录") == 0) //表示用户登录
{
out1.write("2#"+f3.getText()+"#"+password.getText()+"/n"); //发送用户名和密码加上功能码
out1.flush();
in1 = new BufferedReader (new InputStreamReader(socket.getInputStream())); //接收服务器回复信息
String str2 = in1.readLine();
if(str2.compareTo("yes")==0) //// 登录成功
{
start.play();
label2.setText("已登录");
command2.setText("离开");
f3.setEditable(false);
password.setEditable(false);
command3.setEnabled(true);
command4.setEnabled(true);
command5.setEnabled(true);
command6.setEnabled(false);
command7.setEnabled(true);
JOptionPane.showMessageDialog(null,"登录成功");
client = new clientthread(); //客户端产生接收服务器信息线程
client.start();
}
else
{
fail.play();
JOptionPane.showMessageDialog(null,"非法用户,登录失败");
f3.setText("");
password.setText("");
}
}
else //用户离线
{
try
{
leave.play();
command1.setEnabled(true);
f1.setEditable(true);
f2.setEditable(true);
f3.setEditable(true);
password.setEditable(true);
command2.setEnabled(false);
command3.setEnabled(false);
command4.setEnabled(false);
command5.setEnabled(false);
command6.setEnabled(false);
command7.setEnabled(false);
tal.setText("");
combobox.removeAllItems();
label2.setText("未登录");
command1.setText("连接");
command2.setText("登录");
JOptionPane.showMessageDialog(null,"您已和服务器断开连接/n如要再次连接请首先连接上服务器");
f3.setText("");
password.setText("");
if(socket.isConnected() == true) //如果连接上
{
out1.write("6#"+"/n"); //给服务器发送下线信息
out1.flush();
}
System.exit(0);
}
catch (Exception f)
{
}
}
}
catch(Exception g){}
}
if(e.getSource() == command3) ////////////////////发送消息
{
String str1 = f4.getText();
out1.write("3#"+combobox.getSelectedItem()+"#"+str1+"/n");
out1.flush();
f4.setText("");
tal.append("发送给 "+combobox.getSelectedItem()+" : "+str1+"/n");
}
if(e.getSource() == command4) //查找好友
{
out1.write("4#/n"); //发送查找好友的功能码
out1.flush();
}
if(e.getSource() == command5) //添加好友
{
String temp = f5.getText();
combobox.addItem(temp);
JOptionPane.showMessageDialog(null,"您已添加"+temp+"为好友/n可从下拉菜单中选择");
f5.setText("在此键入好友名字");
}
if(e.getSource() == command6) ///////////////////注册新用户
{
final JDialog dialog = new JDialog(this);
Container cp = dialog.getContentPane();
cp.setLayout(new GridLayout(3,2,5,5));
cp.add(new JLabel("用户名"));
final JTextField t1 = new JTextField();
cp.add(t1);
cp.add(new JLabel("密码"));
final JTextField t2 = new JTextField();
cp.add(t2);
dialog.setSize(200,150);
dialog.setLocation(500,500);
JButton command = new JButton("确定");
cp.add(command);
dialog.show();
command.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
try{
out1.write("1#"+t1.getText()+"#"+t2.getText()+"/n"); //发送用户输入的名字和密码
out1.flush();
String str2 = in1.readLine(); //读取服务器返回信息
if(str2.compareTo("1")==0) //如果注册成功
{
JOptionPane.showMessageDialog(null,"注册成功");
dialog.dispose();
}
else //注册失败
{
JOptionPane.showMessageDialog(null,"该用户名已存在");
}
}
catch(Exception f){}
}
});
}
if(e.getSource() == command7) //删除好友
{
String temp = (combobox.getSelectedItem()).toString();
combobox.removeItem(combobox.getSelectedItem());
JOptionPane.showMessageDialog(null,temp+"已被删除");
}
}//actionperformed() ends
class clientthread extends Thread //接收服务器信息的线程
{
clientthread()
{
}
public void run()
{
try{
IP = f1.getText();
port = Integer.parseInt(f2.getText());
in1 = new BufferedReader (new InputStreamReader(socket.getInputStream()));
while(true)
{
String str = in1.readLine();
if(str==null)
{
//in1.close();
}
else
{
msg.play();
tal.append(str+"/n");
}
}
}
catch(Exception e){}
}
}
class label extends Thread
{
ImageIcon icon[] = new ImageIcon[2];
label()
{
icon[0]=new ImageIcon("1.gif");
icon[1]=new ImageIcon("2.gif");
}
public void run()
{
int i=0,j=0;
String str = "王兆东的聊天室";
(getFrames())[0].setTitle(str);
while(true)
{
try{
sleep(200);
}
catch(Exception e){}
la1.setIcon(icon[i]);
i++;
if(i==2)
i=0;
Date dat = new Date();
String da = dat.toString();
time_lab.setText(da.substring(10,19));
}//while ends
}//run ends
}//class label ends
}