package wuclient;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class WuClient {
private static final int WIDTH = 500;
private static final int HEIGHT = 400;
JTextArea jta = new JTextArea();
JTextField jtf = new JTextField(30);
JButton jb = new JButton("Send");
// JPanel topPane = new JPanel();
JPanel bottomPane = new JPanel();
JFrame frame = new JFrame();
JScrollPane jsp = new JScrollPane();
MenuBar menubar = new MenuBar();
Menu choose = new Menu("选项");
Menu about = new Menu("关于");
Menu help = new Menu("帮助");
MenuItem menuItem1 = new MenuItem("发送文件");
MenuItem menuItem2 = new MenuItem("关于这个软件");
MenuItem menuItem3 = new MenuItem("查看帮助");
Socket ss = null;
DataInputStream dis = null;
DataOutputStream dos = null;
private boolean beConnected = false;
String ipAddress = null;
String Clientname = null;
String appendString = "";
public WuClient(String ipAddress,String Clientname) {
frame.setTitle("Wuxuehong-pc");
frame.setMenuBar(menubar);
menubar.setFont(new Font("Srief",Font.BOLD|Font.ITALIC,15));
menubar.add(choose);
menubar.add(about);
menubar.add(help);
//choose.setFont(new Font("Srief",Font.BOLD|Font.ITALIC,15));
//about.setFont(new Font("Srief",Font.BOLD|Font.ITALIC,15));
choose.add(menuItem1);
about.add(menuItem2);
help.add(menuItem3);
menuItem1.addActionListener(new MenuListener());
menuItem2.addActionListener(new MenuListener());
menuItem3.addActionListener(new MenuListener());
frame.setSize(WIDTH, HEIGHT);
frame.setLocation(200, 200);
jta.setBackground(Color.pink);
jta.setEditable(false);
jta.setWrapStyleWord(true);
jta.setLineWrap(true);
jsp.setViewportView(jta);
jta.setFont(new Font("Srief",Font.BOLD|Font.ITALIC,15));
jtf.setFont(new Font("Srief",Font.BOLD,15));
bottomPane.add(jtf);
bottomPane.add(jb);
bottomPane.setBackground(Color.pink);
frame.add(jsp);
frame.add(bottomPane, BorderLayout.SOUTH);
frame.setVisible(true);
jb.addActionListener(new JbListener());
jtf.addActionListener(new JbListener());
this.Clientname = Clientname;
this.ipAddress = ipAddress;
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
//dos.writeUTF(Clientname+"离开了");
ss.close();
dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
beConnected = false;
}
});
connect();
new Thread(new ClientThread()).start();
new Thread(new Repeat()).start();
try {
dos.writeUTF(Clientname+"加入了聊天室");
} catch (IOException e1) {
e1.printStackTrace();
}
}
public void connect() {
//InetAddress inetaddress = null;
try {
//inetaddress = InetAddress.getByName("wuxuehong-pc");
ss = new Socket(ipAddress, 8800);
dis = new DataInputStream(ss.getInputStream());
dos = new DataOutputStream(ss.getOutputStream());
System.out.println("connected successfullly!");
beConnected = true;
} catch (UnknownHostException e) {
JOptionPane.showMessageDialog(null, "Error!");
} catch (IOException e) {
}
}
class JbListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("sending....");
jta.setCaretPosition(jta.getText().length());
String str = jtf.getText();
try {
dos.writeUTF(Clientname+" 说 : "+str);
dos.flush();
} catch(NullPointerException e3){
System.out.println("服务器未启动!");
}catch (SocketException e2){
System.out.println("服务器端已经关闭!");
}catch (IOException e1) {
e1.printStackTrace();
}
//System.out.println(str);
jtf.setText(null);
}
}
class MenuListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()== menuItem1){
//int k=0;
System.out.println("这是发送文件的选项");
//sendFile();
//if(JOptionPane.showConfirmDialog(null,
// "choose one", "choose one", JOptionPane.YES_NO_OPTION)!=1)
//receiveFile();
}
else if(e.getSource()==menuItem2){
JOptionPane.showMessageDialog(null, "开发日期:2009-3-30 作者:武学鸿 功能:多人聊天室");
}
else if(e.getSource()==menuItem3){
JOptionPane.showMessageDialog(null, "如果发现程序退出不了,那就打开任务管理器关闭javaw.exe进程!");
}
}
}
/*
public void sendFile(){
String filename;
String filemulu;
// jta.setText("");
FileDialog s=new FileDialog(new JFrame());
s.setMode(FileDialog.LOAD);
s.setVisible(true);
filemulu=s.getDirectory();
filename=s.getFile();
if(filename!=null)
{
try {
FileReader fr = new FileReader(filemulu+filename);
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
while(str!=null)
{
appendString = appendString+str;
dos.writeUTF(str+"\n");
str = br.readLine();
}
}catch(IOException ioexception){
}
}
}
public void receiveFile(){
String filename;
String filemulu;
// jta.setText("");
FileDialog s=new FileDialog(new JFrame());
s.setMode(FileDialog.SAVE);
s.setVisible(true);
filemulu=s.getDirectory();
filename=s.getFile();
if(filename!=null)
{
try {
FileOutputStream fos=new FileOutputStream(filemulu+filename);
OutputStreamWriter osw=new OutputStreamWriter(fos);
//String str = dis.readUTF();
System.out.println("hehe");
osw.write(appendString);
osw.flush();
osw.close();
}catch(IOException ioexception){
System.out.println("传输成功!");
}
}
}
*/
class ClientThread implements Runnable {
public void run() {
try {
while (beConnected) {
String str = dis.readUTF();
System.out.println("receiving..." + str);
jta.setText(jta.getText() + str + "\n");
}
} catch (IOException e) {
System.out.println("与服务器断开了");
}
}
}
class Repeat implements Runnable { //用于使文本区中的滚动条显示到何时位置
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
jta.setCaretPosition( jta.getText().length());
jta.setCaretPosition( jta.getText().length());
}
}
}
}