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
}